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
Related
table user
+-----+-------+----------+------------+
| id | name | gender | computer |
+-----+-------+----------+------------+
| 1 | ben | 1 | 3 |
+-----+-------+----------+------------+
table translate
+-----+--------+
| id | trans |
+-----+--------+
| 1 | boy |
| 2 | girl |
| 3 | pc |
| 4 | mac |
+-----+--------+
final result
+-----+-------+----------+----------+
| id | name | gender | computer |
+-----+-------+----------+----------+
| 1 | ben | boy | pc |
+-----+-------+----------+----------+
I have 2 tables, one is user's data, the other is translate.
I want to use translate table to translate user's gender & computer's values
anyone know how to achieve this?
Use JOIN.
Query
select
t1.`id`,
t1.`name`,
t2.`trans` as `gender`,
t3.`trans` as `computer`
from `user` t1
join `translate` t2
on t1.`gender` = t2.`id`
join `translate` t3
on t1.`computer` = t3.`id`;
The way you've presented this, it doesn't appear to be a translation problem (in the sense of language), but rather a standard database partition problem. A more standard solution would be to define your entities each in their own tables:
user
+-----+--------+------------+--------------+
| id | name | genderId | computerId |
+-----+--------+------------+--------------+
| 1 | ben | 1 | 1 |
+-----+--------+------------+--------------+
gender
+-----+--------+
| id | name |
+-----+--------+
| 1 | boy |
+-----+--------+
| 2 | girl |
+-----+--------+
computer
+-----+--------+
| id | name |
+-----+--------+
| 1 | pc |
+-----+--------+
| 2 | mac |
+-----+--------+
...and then your query reads:
SELECT *
FROM user
INNER JOIN gender ON user.genderId = gender.id
INNER JOIN computer ON user.computerId = computer.id
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 .
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;
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 have two tables where I'm trying to select rows where a common field between those tables matches exactly, however it's proving difficult to write the query. Here is a simplified version:
The tables look like this (simplified):
T1:
id, name, sn
T2:
id, location, sn
I'm trying to get t1.name and t2.loc together only where t1.sn=t2.sn. The sn field is unique in both, and so at the most, only 1 record will match between tables. In t1, all records have a sn field value, however in t2 about 30% of them have NULL for sn. So, I an expecting the join to produce somewhat fewer rows than t1 has.
How would I do the join?
Thanks.
Sample data:
t1:
+---+--------+-------+-----+
| id| name | sn | ... |
+---+--------+-------+-----+
| 1 | thing1 | 12345 | |
| 2 | thing2 | 10000 | |
| 3 | thing3 | 33445 | |
| 4 | thing4 | 99223 | |
+---+--------+-------+-----+
T2:
+----+--------+-------+-----+
| id | loc | sn | ... |
+----+--------+-------+-----+
| 90 | here | 12345 | |
| 92 | there | NULL | |
| 96 | near | 33445 | |
| 99 | far | 99223 | |
+----+--------+-------+-----+
Result:
+--------+-------+-------+
| name | loc | sn |
+--------+-------+-------+
| thing1 | here | 12345 |
| thing3 | near | 33445 |
| thing4 | far | 99223 |
+--------+-------+-------+
SELECT
t1.name AS name,
t2.loc AS loc,
t1.sn AS sn
FROM t1
INNER JOIN t2 ON t1.sn=t2.sn