I have 3 tables that i have left joined but I would like to add a field from the table 3 which is inner joined with table 2.
Table1
id
name
surname
table2_fk
Table2
id
entry_name
entry_code
table3_fk
Table3
id
type_name
type_desc
SELECT `Name`, `Type Description`
(SELECT
Table1.name AS `Name`,
Table1.surname AS `Surname`,
t2.entry_name AS `Entry`,
t2.entry_code AS `Entry Code`,
t3.type_name AS `Type Name`,
t3.type_desc AS `Type Description`
FROM Table1
LEFT JOIN table2 AS t2 ON Table1.table2_fk = t2.id
LEFT JOIN table3 AS t3 ON Table2.table3_fk = t3.id
) as subquery
My desired outcome is to have t2 inner join t3 and get field from table 1 and table 3
Based on my subquery I would like the Name and Type Description to show
If you want to INNER JOIN tables table2 and table3 and then LEFT JOIN to table1 then do this:
SELECT t1.name, t.type_desc AS `Type Description`
FROM Table1 AS t1
LEFT JOIN (
SELECT t2.id, t3.type_desc
FROM table2 AS t2
INNER JOIN table3 AS t3 ON t2.table3_fk = t3.id
) AS t ON t1.table2_fk = t2.id
But this could be perhaps expressed just as:
SELECT t1.name, t3.type_desc AS `Type Description`
FROM Table1 AS t1
LEFT JOIN table2 AS t2 ON t1.table2_fk = t2.id
LEFT JOIN table3 AS t3 ON t2.table3_fk = t3.id
I presume you are trying to left join table1 with the result of inner join between table2 and table3
select t1.name, t_res.type_desc from table1 t_1
left join (
select t_2.id, t_3.type_desc from table2 t_2
inner join table3 t_3 on t_2.table3_fk = t_3.id
) as t_res on t_1.table2_fk = t_2.id
Related
i have 4 tables: table1, table2, table3, table4.
table1= user_id, username
table2= tbl2_user_id, tbl2_name
table3= tbl3_user_id, tbl3_name
table4= tbl4_user_id, tbl4_name
i want to select the users in table1 if they also are in one of: (table2 OR table3 OR table4).
example:
table1=user1, user2, user3, user4, user5, user6, user7, user8, user9, user10.
table2=user2, user5, user7.
table3=user3.
table4=user4, user9.
[result of table1 select: user2, user3, user4, user5, user7, user9].
I tried this code and did NOT work:
select t1.username
from table1 t1
INNER JOIN table2 t2
ON t1.user_id = t2.tbl2_user_id
INNER JOIN table3 t3
ON t1.user_id = t3.tbl3_user_id
INNER JOIN table4 t4
ON t1.user_id = t4.tbl4_user_id
Thank you,
INNER JOIN is not the correct way, that filters only when all 3 are met, you need a LEFT JOIN and a WHERE condition
A Left Join to not filter the query, and then a WHERE to see if any of them are valid
select t1.username
from table1 t1
LEFT JOIN table2 t2
ON t1.user_id = t2.tbl2_user_id
LEFT JOIN table3 t3
ON t1.user_id = t3.tbl3_user_id
LEFT JOIN table4 t4
ON t1.user_id = t4.tbl4_user_id
WHERE
(
t2.tbl2_user_id is NOT NULL OR
t3.tbl3_user_id is NOT NULL OR
t4.tbl4_user_id is NOT NULL
)
one table t1 has one unique id u1
another table t2 has three unique_id u2a, u2b, u2c
How to make an inner join between two tables so that
u1 of table t1 join either u2a, u2b or u2c of table t2.
I don't think this is a best practice but try:
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/116
If you want the name in order you can add order by name desc; in the end of the query.
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c
order by name desc;
I have 2 tables in MySQL
table1
ID|name|group_1|group_2|group_3
Table2
ID|group_name|Group_location
I want to get table 1 ID and show all the data and not tableIDs.
I'm trying to use a join like so
SELECT table1.name, table2.group_1, table1.group_2, table1.group_3
FROM tabl1 JOIN table2 on table1.group_1 = table2.ID AND
table1.group_2 = table2.ID AND table1.group_3 = table2.ID
WHERE table1.ID IN (868)
ORDER BY FIELD(table1.ID,868);
It's only returning the IDs in the result but I want it to return the table2 group_name
Is this what you want?
SELECT t1.name, t21.group_1, t22.group_2, t23.group_3
FROM table1 t1 LEFT JOIN
table2 t21
ON t1.group_1 = t21.ID LEFT JOIN
table2 t21
ON t1.group_2 = t22.ID LEFT JOIN
table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER BY FIELD(t1.ID, 868);
This uses LEFT JOIN in case any of the reference columns are NULL.
Try this;
SELECT t1.name
, t21.group_name
, t22.group_name
, t23.group_name
FROM table1 t1
LEFT
JOIN table2 t21
ON t1.group_1 = t21.ID
LEFT
JOIN table2 t21
ON t1.group_2 = t22.ID
LEFT
JOIN table2 t21
ON t1.group_3 = t23.ID
WHERE t1.ID IN (868)
ORDER
BY FIELD(t1.ID, 868);
I have so far written the following mysql query which is joining 4 tables as follows:
SELECT
T3.fault, t4.name
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
order by count(*) desc;
Below is sample Data for table 2 which has item_id as PK
Example query: select * from table2 where item_id = '15907';
item_id host
15907 abc.com
7303 cde.com
7304 abcd.com
7305 cdedf.com
I have now recently added a table table5 which looks as below and has item_id as PK. I want to join table5 with table2 on item_id and want to retrieve value for restoreid also in my final query.
item_id restoreId
15907 12342
7303 12342
7304 14342
7305 14342
How to implement join between table5 and table2 on item_id? My select query should also retrieve T5.restoreId along with T3.fault, t4.name
select your_table.fault,your_table.name,t5.restoreid
from (
SELECT
T3.fault, t4.name,t2.item_id
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON 3.runname_id = T4.id
) as your_table left join table5 t5 on your_table.item_id = t5.item_id
SELECT
T3.fault, t4.name, T5.restoreid
FROM table2 T2
INNER JOIN (
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON a.item_id = b.item_id AND a.submit_id < b.submit_id
WHERE b.submit_id IS NULL
) T1 ON T1.item_id = T2.item_id
INNER JOIN table3 T3 ON T1.id = T3.run_id
LEFT JOIN table4 T4
ON T3.runname_id = T4.id
LEFT JOIN table5 ON T2.item_id = T5.item_id;
Whats the right MySQL query with two LEFT JOINs between three tables?
SELECT COUNT(1) FROM TABLE1 WHERE T1_ID NOT IN (
SELECT T2.T2_ID FROM TABLE2 T2 LEFT JOIN
TABLE3 T3 ON T2.T2_ID=T3.T3_ID WHERE T3.T3_ID IS NULL )
Something like
SELECT COUNT(1) FROM TABLE1 T1 LEFT JOIN TABLE2 T2 ON T1.T1_ID=T2.T2_ID
LEFT JOIN TABLE3 T3 ON T2.T2_ID=T3.T3_ID WHERE T2.T2_ID IS NULL AND
T3.T3_ID IS NULL
For performance I would do something like
SELECT
COUNT(1)
FROM
TABLE1
LEFT JOIN (
SELECT
T2.T2_ID id
FROM
TABLE2 T2
LEFT JOIN TABLE3 T3 ON
T2.T2_ID=T3.T3_ID
WHERE
T3.T3_ID IS NULL
) t1 ON
t1.id = table1.t1_id
WHERE
t1 is null;