I have a table like this
SparePart
+-------------+------------------------+--------+---------+
| sparePartID | name | price | modelID |
+-------------+------------------------+--------+---------+
| 12V | Preheat Start Relay | 1470 | WADR40E |
| 12V. | Instrument Assembly | 1290 | WADR40E |
| 12V.. | Motor Module(360) | 17810 | WADR40E |
| 40CC | HST Assembly | 264840 | WADR40E |
| 4L88 | Oil Filter Core | 1200 | WADR40E |
| 4L88. | Diesel oil Filter Core | 260 | WADR40E |
| 4SB1490 | Belt | 9930 | WADR40E |
| 50*2.65 | Axeal -O Ring | 80 | WADR40E |
| 60*85*10 | Oil seal | 1180 | WADR40E |
| 9J-5-1605 | Joint Belt | 8960 | WADR40E |
+-------------+------------------------+--------+---------+
and MainStock
+-------------+-----+------------+-------------+
| originalQty | qty | shipmentID | sparePartID |
+-------------+-----+------------+-------------+
| 20 | 20 | RnsttFOY | RT125-03001 |
| 10 | 10 | SHPMT78 | RT125-03001 |
| 8 | 8 | RH987ho | 12V |
| 0 | 0 | RH987ho | 4SB1490 |
+-------------+-----+------------+-------------+
So I use a query like this to count all stock from all shipments
SELECT SUM(`qty`) FROM MainStock WHERE sparePartID='RT125-03001';
I want to loop through each of sparePartID from SparePart table and get table results. I tried something like this.
SELECT SUM(`qty`)
-> FROM MainStock, SparePart
-> WHERE sparePartID=SparePart.sparePartID;
But I get a message saying
ERROR 1052 (23000): Column 'sparePartID' in where clause is ambiguous
So how can I accomplish this?
You really don't need a loop, just a simple join and GROUP BY:
SELECT SP.sparePartID, SUM(qty) as qty
FROM SparePart SP
LEFT JOIN MainStock MS
ON SP.sparePartID = MS.sparePartID
GROUP BY SP.sparePartID
You have to add tables names to every column or better use aliases for your tables:
SELECT SUM(`qty`)
FROM MainStock ms, SparePart sp
WHERE ms.sparePartID=sp.sparePartID;
Group by sp.sparePartID
You are getting error because, both table contains the column sparePartId. So lets give the table name also to avoid ambiguity .
SELECT Mainstock.sparePartId, SUM(`qty`)
FROM MainStock, SparePart
WHERE Mainstock.sparePartID=SparePart.sparePartID;
Related
I am trying to select data from multiple tables. The main table is Notices.
Table: Notices
+-------+-------------+----------------+
| id | notice_code | company_number |
+-------+-------------+----------------+
| 96008 | 2410 | 09844265 |
| 96014 | 2450 | 02640968 |
| 96032 | 2443 | 03666759 |
+-------+-------------+----------------+
I have to select related information for the rows for Table Notice from different tables. Below are the other 4 tables and their relation with table Notices
Table Companies has a direct relation with Table Notices
Table: Companies
Companies.Company_number = Notices. Company_ Number
+----------------+--------------+-------+
| company_number | company_name | sic1 |
+----------------+--------------+-------+
| 02640968 | XYZ Logistic | 28220 |
| 03666759 | OPQ Logistic | 41100 |
| 09844265 | ABC Logistic | 49410 |
+----------------+--------------+-------+
Table Sic_codes doesn’t have a direct relation with Table Notices. But it has with Table Companies.
Table: Sic_Codes.
Companies.Sic1 = Sic_code.Code
+-------+----------------+
| code | sector |
+-------+----------------+
| 28220 | Manufacture |
| 41100 | Construction |
| 49410 | Transportation |
+-------+----------------+
Table Insovency_Practionar does not have a direct relation with Table Notices. There is another Table Notice_insolvency_practitionar_ID to create a relation between these two tables Table Insovency_Practionar and Table Notices
Table: Notice_insolvency_practitionar_ID .
Notice_insolvency_practitionar_ID. Notice_ID = Notices. ID
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96008 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
Table: Insovency_Practionar .
Insovency_Practionar.ID = Notice_insolvency_practitionar_ID. Insolvency_Practiotionar_ID
+------+---------+
| id | name |
+------+---------+
| 548 | Charlie |
| 725 | Bill |
| 1048 | Andrew |
+------+---------+
My expected output is the following: where company name will be coming from table company; sic1 and sector will come from table sic_code and practitioner will come from table Insovency_Practionar
+----------------+--------------+-------+----------------+--------------+
| company_number | company_name | sic1 | sector | practitioner |
+----------------+--------------+-------+----------------+--------------+
| 9844265 | ABC Logistic | 49410 | Transportation | Andrew |
| 2640968 | XYZ Logistic | 28220 | Manufacture | Bill |
| 3666759 | OPQ Logistic | 41100 | Construction | Charlie |
+----------------+--------------+-------+----------------+--------------+
I have used LEFT Join in my QUERY.
Here is my query
SELECT n.company_number
, c.company_name
, c.sic1
, s.sector
,i.name practitioner
FROM notices n
LEFT JOIN companies c
ON c.company_number = n.company_number
LEFT JOIN sic_codes s
ON s.code = c.sic1
LEFT JOIN notice_insolvency_practitioners ni
ON ni.notice_id = n.id
LEFT JOIN insolvency_practitioners i
ON i.id = ni.insolvency_practitioner_id
where n.notice_code =2410
Here is my SQL Fiddle. http://sqlfiddle.com/#!9/4887e2/2
I wanted to know if my query was right. Or is there any other better way to write the query. As initially, the query gave me wrong result when I was testing.
Update: I have figured out there was a duplicate entry. And that was the reason for not getting the expected output. I have now corrected that. But still want to know if my query is right or is there a better way to write the query
I have figured out what the problem was. Thanks to #Viney
<pre>
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96008 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
</pre>
duplicate entry for notice id 96008. It should be like
<pre>
+-----------+----------------------------+
| notice_id | insolvency_practitioner_id |
+-----------+----------------------------+
| 96008 | 1048 |
| 96014 | 725 |
| 96032 | 548 |
+-----------+----------------------------+
</pre>
I have also edited My question and have reduced the extra columns to make it readable for users
i am watching a tutorial. There is a code which i don't understand what is supposed to do.
$sql = 'SELECT p.*,
a.screen_name AS author_name,
c.name AS category_name
FROM
posts p
LEFT JOIN
admin_users a ON p.author_id = a.id
LEFT JOIN
categories c ON p.category_id = c.id
WHERE
p.id = ?';
I read about the left joins but i didn't understand them. Can somebody please explain me the code i shared.
Thanks in advance!
Imagine you have two tables. One that stores the information about the programmers on your website, and the other table that keeps track of their online purchases.
PROGRAMMERS Table
+--------------------------------------------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Desire | 32 | 123 fake s| 3000.00 |
| 2 | Jamin | 25 | 234 fake s| 2500.00 |
| 3 | Jon | 23 | 567 fake s| 2000.00 |
| 4 | Bob | 30 | 789 fake s| 1500.00 |
| 5 | OtherGuy | 31 | 890 fake s| 1000.00 |
| 6 | DudeMan | 32 | 901 fake s| 500.00 |
+--------------------------------------------+
PURCHASES Table
+---------------------------------------------+
| ORDER_ID | PROG_ID | DATE | PRICE |
+-------------+---------+---------------------|
| 1 | 1 | 1-1-2017 | 100 |
| 2 | 2 | 1-2-2017 | 200 |
| 3 | 6 | 1-3-2017 | 300 |
+---------------------------------------------|
You decide you need to make a new table to consolidate this information to a table that contains
certain columns you want.
For example, you figure it would be nice for shipping purposes to have a table
that has the ID, the NAME, the PRICE, and the DATE columns.
Currently, the tables we have don't display all of that in a single table.
If we were to LEFT JOIN these tables, we would end up filling the desired columns
with NULL values where there is no information to join.
SELECT ID, NAME, PRICE, DATE
FROM PROGRAMMERS
LEFT JOIN PURCHASES
ON PROGRAMMERS.ID = PURCHASES.PROG_ID;
Notice that I'm selecting the columns I want from the starting table, then joining the right table
even though there might be missing information.
RESULTING TABLE
+-------------------------------------+
| ID | NAME | PRICE | DATE |
+----+----------+-----------------+---+
| 1 | Desire | 100 | 1-1-2017 |
| 2 | Jamin | 200 | 1-2-2017 |
| 3 | Jon | NULL | NULL |
| 4 | Bob | NULL | NULL |
| 5 | OtherGuy | NULL | NULL |
| 6 | DudeMan | 300 | 1-3-2017 |
+-------------------------------------+
For a visual representation of the difference between SQL JOINs check out
https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins .
For this question I have created a simple example that illustrates what I am asking.
Say I had a table called 'books'
+----+----------------------------+-----------+
| pk | title | author_id |
+----+----------------------------+-----------+
| 1 | The Lost Symbol | 1 |
| 2 | Follow Us Home | 2 |
| 3 | The Man in the High Castle | 3 |
+----+----------------------------+-----------+
(table a)
And another table called 'shops', that had a list of shops that sold each book:
+----+---------+-------------+-------+
| pk | book_id | shop_name | price |
+----+---------+-------------+-------+
| 1 | 1 | WHSmith | 5.00 |
| 2 | 1 | Waterstones | 7.00 |
| 3 | 1 | Amazon | 2.50 |
| 4 | 2 | WHSmith | 4.00 |
| 5 | 2 | Borders | 4.50 |
+----+---------+-------------+-------+
(table b)
If I do a simple select that grabs a book and all of the places it is sold using a join such as:
SELECT
books.*,
shops.shop_name,
shops.price
FROM
books
JOIN shops ON books.pk = shops.book_id
WHERE
book.book_name = "The Lost Symbol"
I would get results such as below:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table c)
However, I would LIKE to receive results like this:
+----+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+----+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| 1 | The Lost Symbol | 1 | WHSmith | 5.00 |
| 1 | The Lost Symbol | 1 | Waterstones | 7.00 |
| 1 | The Lost Symbol | 1 | Amazon | 2.50 |
+----+-----------------+-----------+-------------+-------+
(table d)
I.e. the first row is just the result of left outer join and the rest of the results are the the inner join.
An even more desired outcome is:
+------+-----------------+-----------+-------------+-------+
| pk | title | author_id | shop_name | price |
+------+-----------------+-----------+-------------+-------+
| 1 | The Lost Symbol | 1 | NULL | NULL |
| NULL | NULL | NULL | WHSmith | 5.00 |
| NULL | NULL | NULL | Waterstones | 7.00 |
| NULL | NULL | NULL | Amazon | 2.50 |
+------+-----------------+-----------+-------------+-------+
(table e)
Having shop_name and price concatenated and grouped in a single row seems not to work as it only does the first result from shops instead of all of them, also in my real world scenario, I have punctuation in the data so have to be careful with the separator.
So how would I get the result of table e?
You can use UNION ALL to build the required result set:
SELECT pk, title, author_id, NULL AS shop_name, NULL AS price
FROM books
WHERE books.title = "The Lost Symbol"
UNION ALL
SELECT NULL AS pk, NULL AS title, NULL AS author_id, shops.shop_name, shops.price
FROM books
JOIN shops ON books.pk = shops.book_id
WHERE books.title = "The Lost Symbol"
The first part of the union operation returns the first row of the result, i.e. the book title. The second part returns the rest of the rows, i.e.the shop names.
Demo here
I have a table which requires me to pair certain rows together using a unique value that both the rows share.
For instance in the below table;
+--------+----------+-----------+-----------+----------------+-------------+
| id | type | member | code | description | matching |
+--------+----------+-----------+-----------+----------------+-------------+
| 1000 |transfer | 552123 | SC120314 | From Gold | |
| 1001 |transfer | 552123 | SC120314 | To Platinum | |
| 1002 |transfer | 833612 | SC120314 | From silver | |
| 1003 |transfer | 833612 | SC120314 | To basic | |
| 1004 |transfer | 457114 | SC150314 | From Platinum | |
| 1005 |transfer | 457114 | SC150314 | To silver | |
| 1006 |transfer | 933276 | SC180314 | From Gold | |
| 1007 |transfer | 933276 | SC180314 | From To basic | |
+--------+----------+-----------+-----------+----------------+-------------+
basically What i need the query / routine to do is find the rows where the value in the 'member' column for each row match. Then see if the values in the 'code' column for the same found rows also match.
If both columns for both rows match, then assign a value to the 'matching' column for both rows. This value should be the same for both rows and unique to only them.
The unique code can be absolutely anything, so long as it's exclusive to matching rows. Is there any query / routine capable of carrying this out?
I'm not sure I understand the question correctly, but if you like to pick out and update rows where the code and member columns matches and set matching to some unique value for each of the related rows, I believe this would work:
UPDATE <table> A
INNER JOIN (SELECT * FROM <table>) B ON
B.member = A.member && B.code = A.code && A.id <> B.id
SET A.matching = (A.id + B.id);
The matching value will be set to the sum of the id columns for both rows. Notice that updating the matching field this way will not work if there are more than two rows that can match.
Running the above query against your example table would yield:
+------+----------+--------+----------+---------------+----------+
| id | type | member | code | description | matching |
+------+----------+--------+----------+---------------+----------+
| 1000 | transfer | 552123 | SC120314 | From Gold | 2001 |
| 1001 | transfer | 552123 | SC120314 | To Platinum | 2001 |
| 1002 | transfer | 833612 | SC120314 | From Silver | 2005 |
| 1003 | transfer | 833612 | SC120314 | To basic | 2005 |
| 1004 | transfer | 457114 | SC150314 | From Platinum | 2009 |
| 1005 | transfer | 457114 | SC150314 | To silver | 2009 |
| 1006 | transfer | 933276 | SC180314 | From Gold | 2013 |
| 1007 | transfer | 933276 | SC180314 | From To basic | 2013 |
+------+----------+--------+----------+---------------+----------+
I can give you a simple query what can do what you need.
tst is the name of the table.
SELECT *, COUNT( t2.id ) as matching FROM tst t LEFT JOIN tst t2 ON t2.member = t.member GROUP BY t.id
I'm using VB.Net 2010 and MySQL.
I have two tables in MySQL database 'CAR' and 'CAR_RENT'.
From the VB.Net I want to do the following calculations:
I want to calculate the total_fee Column in CAR_RENT. Which can be multiplying the rental_fee column from 'CAR' table with the date difference of Issue_date and return_date from 'CAR_RENT' table.
I want to calculate the penalty_fee column of 'CAR_RENT' table by finding the exceeded date from the return_date. That should be rental_fee*number_of_exceeded_date for specified client.
That should be automatically calculated when the program is run.
I know that the code I tried is completely not formal way so no need to post it here. Please I need your help??
TABLE:CAR
+-----------+----------+---------------+--------+----------------+
| Car_id | Plate_no | Model | color | Rental_fee_day |
| 100 | 25534 | Tesla Model S | Black | $3500 |
| 101 | 25535 | Audi A6 | Black | $2100 |
| 103 | 35625 | BMW 3 Series | silver | $2000 |
+-----------+----------+---------------+--------+----------------+
TABLE:CAR_RENT
+-----------+--------+------------+-------------+-----------+-------------+
| Client_id | Car_id | Issue_date | Return_date | Total_fee | Penalty_fee |
+-----------+--------+------------+-------------+-----------+-------------+
| 1 | 103 | 2014-02-01 | 2014-02-10 | | |
| 1 | 100 | 2014-02-01 | 2014-02-15 | | |
| 3 | 101 | 2014-02-18 | 2014-02-30 | | |
+-----------+--------+------------+-------------+-----------+-------------+
you should check here this is the DateDiff function for mysql.
you can use this and a join to get the info you need....