I have messed with this query for 3 days now and no matter what I cannot get the right result. I'm working on a small database for a friend of mine for his mechanic shop.
I got 3 tables car, jobs and invoices. Everything start with "cars" if you add a car you get car_id for it. Next step would be to add a job to this car. Of course job goes to jobs table and also records car_id. After the job is done you can create an invoice out of it which goes to invoices table and again records car_id.
cars jobs invoices
=======================================================
cID, make, model | jID,cID,job1 | iID,jID,cID,amount
1 audi a6 | 1 1 check | 1 1 1 99.99
2 bmw 750 | 1 2 oil | 2 2 2 56.97
3 saab 95 | 3 3 oil | 3 3 3 30.22
1 audi a6 | 4 1 oli | 4 4 1 22.33
Small draft what the tables look like. And the outcome what I'd like to get should look like this:
ID make model
--------------------------
1. audi a6
Job 1 Invoice 1
Job 4 Invoice 4
2. bmw 750
Job 2 Invoice 2
3. Saab 95
Job 3 Invoice 3
Basically when I click my client list I get a table rows with clients and underneath client I'd like to print links to jobs and invoices.
Quite long post but I hope you understand what I mean.
I've tried different JOINs, the latest that I tried was
SELECT * FROM cars
RIGHT JOIN jobs ON cars.car_id=jobs.car_id
RIGHT JOIN invoices ON cars.car_id=invoices.car_id
ORDER BY cars.car_id;
It kind a worked but it gave me separate rows for each job with same client info. May I'm printing the result wrong. I just don't know any more. Can anybody offer a solution for this? right SQL command html for printing.
Why don't you just make it in a single table with fields like this:
id car job invoices
===============+===============+==============+==============+
1 + fastCar + works + 99.99 +
+ + + +
+ + + +
With statement like this:
CREATE TABLE cars (id int(11) AUTO_INCREMENT, car text(50) default '', job text(50) default '', invoices int(5) default NULL);
Then you can get all car attributes using a query like:
SELECT job FROM cars WHERE car='fastCar';
and pass desired values with another query to a teble that contains
details for the job for example, where you can have job id as needed.
Related
I have a problem to calculate sum of the amount column from some references field in 4 different tables. here is my tables :
First Table (Master) :
ID_1 | Name_1
1 A
2 B
Second table (Master) :
ID_2 | ID_1 | Name_2
1_1 1 A1
1_2 1 A2
2_1 2 B1
2_2 2 B2
Third Table (Transaction) :
ID_trans | ID_2 | trans_name | amount | cpy_ID
trans1 1_1 Rev 123 1400
trans2 2_1 Dir 321 1400
trans3 2_1 Ind 231 1400
trans4 1_2 OTH 234 1400
Fourth Table (report template) :
report_set_id | report_set_name | cpy_ID
set001 Own Apps 1400
set002 Third Party 1400
The main case is I have to create a report with the third table (transaction) as data reference. And the report template has been determined like this :
----------------------------------------------------
| 1 | 2 | TOTAL |------> (1 & 2 first table fields)
----------------------------------------------------
set001 | (data 1) | - | (horizontal sum)
set002 | - | (data 2) | (horizontal sum)
-----------------------------------------------------
TOTAL | (sum of 1)| (sum of 2) |
which is :
(data 1 & data 2) = summary data from transaction table with same ID_2 and put in the column 1 rows (bacause ID_1 is foreign key in the second table)
I know my language is complicated to understand cause actually its hard to explaining by words, but I hope you guys can get what exactly I mean it :D
Can someone give me some advice to solve my problem? Thanks
If this will only ever have 2 data columns (as labelled "1" and "2" in your example) then it will be quite easy to write in SQL and we could do that. But if there will be several data columns, and if there will be a variable number of data columns, then we are into the general "pivot table" question. You can find many discussions on this topic under the [pivot] tag in stackoverflow. My own opinion is that anything but trivial formatting (including almost all pivot tables) is best done in the application. Use SQL to group and aggregate the data, then use a visualisation tool, or your application, to format it. I have written more about this on my website, where I explain a couple of common approaches and why I don't use them.
Eg:
**Category Table**
Catg_id Catg_name
-------------------
1 Bike
2 Car
**Company Table**
Company_id Company_name
--------------------------
1 Bajaj
2 Honda
**Company_category table**
com_catg_id Company_id Category_id
---------------------------------------
1 1 1
2 2 1
3 2 2
** Models table**
Model_id Model_name com_catg_id
----------------------------------------
1 Pulsar 220 1
2 Unicorn 2
3 City 3
**Purchase Table***
Purchase_id Vehicle_No Rate model_id status
-------------------------------------------------------
1 KL 02 AN8306 50000 2 0
2 KL 10 AZ4764 120000 1 1
3 KL 04 AV8578 800000 3 1
These are 4 Database tables using.
I am using ajax for auto complete searching through a single field
eg: searching car, want to list all cars in purchase table of status 1
if searching bike, want to list all bike in purchase table of status 1
search using company name, want to list all vehicle from that company in purchase table of status 1
same as search using model name, vehicle no,rate want to list matched items in purchase table
Please help me and please send a mysql query for implementing this.
Check this tut
This would surely help you.
This tutorial is for single field. It isn't hard to modify the code and use for multiple fields
I have a page that shows restaurant profile data, and one of the data shown is the total checkin count of users to the restaurant
I have a mysql table like: user_checkins which stores the checkin of users into restaurants like:
id | user_id | res_id | checkin_date |
1 | 102 | 5526 | 2016-04-21 03:20:21 |
2 | 165 | 5574 | 2016-04-21 06:35:21 |
3 | 102 | 4565 | 2016-04-24 02:15:30 |
and another table res_checkin_count:
id | res_id | total_checkin_count |
1 | 5526 | 1055 |
after a while many rows will be created in user_checkins, because people checkin frequently
Question : Should I delete the older rows? like create a cronjob that deletes old rows periodically(like daily) for reach restaurant and update the restaurant total_checkin_count number in another mysql TABLE storing only the total_checkin_count of each restaurant? will this consume alot of memory?
or
I keep the rows and let it accumulate and use SELECT COUNT(*) all to get each restaurant total_checkin_count?
EDIT: the user_checkins table actually stores all user checkin for various restaurants, everytime someone visits a 'restaurant_profile' webpage, the SELECT COUNT(*) query will run on the user_checkins table for res_id x, to get the total checkin count of that restaurant, is that redundant?
When you say many rows you need to assess if many is beyond the capabilities of MySQL. In general MySQL should easily be able to handle in the order of 100 million rows per table. Are you expecting to exceed over 100 million rows any time soon? If not, then leave your data alone, it reduces the complexity that would come with an archiving system.
If on the other hand you are expecting more than hundreds of millions of rows on tables, then yes, running a daily job to delete or archive your data can be helpful in keeping your database running well.
seems to me those tables are in MySQL, however I'll just get rid of res_checkin_count is a duplicate of an aggregate function that is the COUNT so you are wasting memory, so there can only be 2 scenarios:
1 your user_checkins table does not have more than 2 million records and you create nuncluster index for Column res_id and will be fine.
2 You have a Monstrous website where you store more than 2 million active records and you create tables per State or per brick (3 to 5 zipcodes) that way you will have distributed records most likely people form TX will search and Query restaurants from TX and so on.
I'm a new in Mysql and I have a complicated problem:
I have a table with "Shops" name in this table there is a ShopID column. The records look like this:
Shop_001
Shop_002...
Every "shopID" refer to a new table with this name, for example there is a table with Shop_0001 name. In this table there is "partnumber" column which mean the parts which are available in this shop.
I send a specific part number to sql server and I want to check all shops in the "Shops" table and return a rows in the "Shop_xxxx" tables which has this specific partnumber.
Unfortunately I have no idea how do I get start on this. Can anybody help me give some instruction or anything on this?
you're looking for a many to many relationship. so you just need 3 tables
1 table is the list of shops
1 table is the list of products
and 1 table is the list of which shops have which products. like this
table1
id|shops
------
1 shop1
2 shop2
3 shop3
table2
id|products
------
1 prod1
2 prod2
3 prod3
4 prod4
5 prod5
table3
id|shop_id|prod_id
-------------------
1 2 3
2 2 1
3 2 2
4 1 3
5 1 4
6 1 5
7 3 2
So for every time a product is added to a shop, an entry is added in table3. This will allow you to query by shops or by products, and you will only ever need 3 tables.
google querying many to many relationships for how to get the list of products for shop1 or the list of shops that have product4 etc.
I'm trying to streamline updating database records. I've investigated several methods and options but any that focus on PHP instead of MySQL would require hundreds if not thousands of queries, that is clearly not desirable. I'm working with three tables, one contains rows that identify various aspects of a product, one that connects the descriptive values to products and finally the products. The descriptive table has a column that is to trigger an update for all products that use it. The trouble I'm having is that the update might require the product use a new description and that description might have to be created and then set in the product connect table. The description table is also used to calculate the new value based off of another entry.
Description Table:
id | unit | type | update
------------------------------------
1 4oz weight
2 3 servings 1
3 7oz weight
4 4 servings
5 0.66 price_per_serving
6 0.49 price_per_oz
Connector Table:
id | description_id | product_id
--------------------------------
1 1 1
2 2 1
3 2 2
4 4 3
Product Table:
id | name | price
-----------------------
1 soup 1.99
2 crackers 2.00
3 chips 0.79
4 candy bar 0.99
So from the example tables I need to find that item 2 in the description table needs updating. Through the connector table I see the soup and crackers have three servings. I need to take the price of each item using entry 2 and divide it by the number of servings. If that new value isn't in the description table (say 0.78 price_per_serving), it needs to be created and the connector table needs to hold the id of the existing matching entry if one exists or the new one that was created if it didn't. The entry update field will now be cleared.
I can perform all of the functions with PHP no problem, as you can see though it would just place an enormous burden on resources.
I've been trying to wrap my head around sub-queries and reading plenty of articles, manuals and other questions on this site but I'm just so overwhelmed I don't know how to get started. I've found queries like the example shown in this post (http://stackoverflow.com/questions/2542571/mysql-case-when-then-returning-wrong-data-type-blob) but my MySQL experience is mostly select, update, insert.