I have three table
table1
userid name
1 A
2 B
3 C
table2
trackid userid track_des
1 2 123
2 3 234
3 3 345
table3
trackid description
1 ABC
2 BCD
I want output like this where userid=3
userid name trackid description track_des
3 C 2 BCD 234
3 C 3 NULL 345
I am using MySQL.
Your table1 looks to contain users and your table3 looks to contain tracks. Your table2 looks like it's a join table describing a many-to-many relationship between users and tracks. It seems the join table may contain references to tracks that don't exist in table3 eg. trackid 3. To produce the output you've shown in your example, begin by selecting the records corresponding to userid 3 and join it to table2 (your many-to-many join table). Next, you should left join to your table containing tracks. You need a left join here because some tracks listed in the join table won't be found but those rows should still be included in the output. The only task remaining is specifying the columns you would like returned. Here is what the complete query looks like:
select
u.*,
ut.trackid,
t.description,
ut.track_des
from table1 as u
inner join table2 as ut
on ut.userid = u.userid
left outer join table3 as t
on t.trackid = ut.trackid
where u.userid = 3;
select
t1.*,
t2.trackid,
t3.description,
t2.track_des
from table1 as t1
inner join table2 as t2
on t2.userid =t1.userid
left join table3 as t3
on t3.trackid = t2.trackid
where t1.userid = 3;
Related
I have two tables. I'm trying to JOIN the sample two tables below with table 1 referencing Table 2 twice. For example if I look at Table 1: Group 2 and Members 7, it should look up the ID in Table 2 and give me an output of:
Group Members Name Name
2 7 Blue Dog
Table 1
Group Members
2 7
3 8
5 10
Table 2
ID Name
1 Green
2 Blue
3 Yellow
4 Orange
5 Red
6 Elephant
7 Dog
8 Cat
9 Chicken
10 Mouse
Any Advice? Thanks
SELECT
Table_1.*,
g.Name,
m.Name
FROM
Table_1
INNER JOIN Table_2 AS g ON Table_1.Group=g.ID
INNER JOIN Table_2 AS m ON Table_1.Group=m.ID
WHERE
Table_1.Group=2
AND Table_1.Member=7
Join with Table 2 twice on different columns.
SELECT t1.*, t2.name AS group_name, t3.name AS member_name
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.group = t2.id
JOIN Table2 AS t3 ON t1.members = t3.id
Hard to tell exactly what you need from that description but aliasing the tables may be what you need. It works like this:
SELECT t1.x, t2_1.y, t2_2.z
FROM table1 AS t1
JOIN table2 AS t2_1 ON t1.whatever = t2_1.whatever
JOIN table2 AS t2_2 ON t1.whatever = t2_2.whatever
...
I have two tables:
Table 1 contains the User ID
Table 2 contains the user ID and other data I would like
The relationship is on the ID in both tables so what I would like to do is the following:
Pull all data from table 2 where a record exists in the id field in table 2 that matches an id in table 1.
Table 1 has other copies so to speak that are specific to other accounts while table 2 contains all the ids for all the other tables which is why (I think) I need a JOIN statement but I'm open to suggestions.
Table 1:
id
123456
Table 2:
id | name | age
123456 | John | 23
651123 | Mary | 22
811561 | Sarah | 21
You can use subquery as:
SELECT *
FROM table2
WHERE ID IN (SELECT ID
FROM table1)
If you need fields from table1 as well then use an inner join like:
SELECT t1.*, t2.name, t2.age
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.id
Your assumption is correct, you need to join:
Select * from table1 inner join table2 on table1.userid = table2.userid
The only question here is if you want to get only id's that appear on both tables (and than use inner join) or also get such that appear only on the first table as well (left join)
You should choose inner join here because table 2 always contain records for table 1.
You can use INNER JOIN here becuase you have relation between both table, query should be this:
SELECT t1.id, t2.name, t2.age
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id
umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]
I have a table with two sets of IDs (dec_id and rec_id). If dec_id is found in Table1, I need to left join that table. If not - left join table2. The same goes for the second column. I also need to know which ID was found in which table.
Here's the abstract structure of Table 1:
dec_id rec_id
-----------------
11 16
23 24
15 31
Tables 2 and 3 are for all intends and purposes identical (apart from the actual data):
id name data1 data2
------------------------------------------
11 a x p
41 b y q
55 c z r
And the logic (abstract):
IF (dec_id IS IN Table1) {
LEFT JOIN Table1 on (dec_id = table1.id);
LEFT JOIN Table2 on (rec_id = table2.id);
FLAG = 1;
} ELSE {
LEFT JOIN Table1 on (rec_id = table1.id);
LEFT JOIN Table2 on (dec_id = table2.id);
FLAG = 0;
}
LEFT JOIN Table3, Table4, ...
I know the result can be achieved in PHP with more than one query, but I'd like to avoid it if possible for the sake of efficiency; unfortunately my SQL knowledge fails me.
Thanks in advance!
Are you looking for something like this?
SELECT COALESCE(t2.id, t3.id) id,
(t2.id IS NOT NULL) flag,
COALESCE(t2.name, t3.name) name,
COALESCE(t2.data1, t3.data1) data1
FROM table1 t1 LEFT JOIN table2 t2
ON t1.dec_id = t2.id LEFT JOIN table3 t3
ON t1.rec_id = t3.id
Here is SQLFiddle demo
At first I thought this may work as a join but I'm not sure if this is really a union command or if even possible. Below is an example of the two tables and each has about 20 more columns of various different data.
Table 1
> id assembly user1 user2 containerID productID packageID
1 line2 Billy John 3794 4892 4589
2 line4 John Doug 7794 6201 7864
Table 2
> item_id name width height weight flag1 flag2
3794 Box 10 10 10 0 1
4892 Lamp 4 6 2 1 1
7864 BigBox 200 200 300 4 5
What I am trying to do is show all of Table 1 but replace the containerID, productID, and packageID with their name from Table 2 using the matching item_id. Tried doing this outside of mysql with foreach but with Table 2 having 30k rows it lags up "just a bit" when trying to display the hundreds of rows from Table 1 and replacing each id with its name equivalent.
To see all the table_1 records, use:
SELECT t1.id, t1.assembly, t1.user1, t1.user2,
t2c.name, t2pr.name, t2pk.name
FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2c ON t2c.item_id = t1.containerid
LEFT JOIN TABLE_2 t2pr ON t2pr.item_id = t1.productid
LEFT JOIN TABLE_2 t2pk ON t2pk.item_id = t1.packageid
You can change those to INNER JOINs, but any row that doesn't match all three will not be in the resultset.
My query uses table aliases, because you have to join to the appropriate TABLE_2 column for each name you want to look up.
try to use something like this:
SELECT id, assembly, user1, user2,
(SELECT name from table2 where item_id = table1.containerID),
(SELECT name from table2 where item_id = table1.productID),
(SELECT name from table2 where item_id = table1.packageID)
FROM table1
ORDER BY id;
You'll need to join each row three times for each item_id
SELECT t1.*, t21.name,t22.name,t23.name FROM table_1 t1
INNER JOIN table_2 t21 ON t1.containerID = t21.itemid
INNER JOIN table_2 t22 ON t1.productId = t22.itemid
INNER JOIN table_2 t23 ON t1.packageID = t23.itemid
Make sure there's an index on table_2's itemid