Adding fifth join in the table - mysql

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;

Related

mysql - select if also is in other tables

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
)

Query Multiple IDs in 1 table row to another table in 1 query

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);

Mysql : Left Join and Inner Join in a subquery

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

Find common users in multiple tables in SQL

I have 5 tables.
I want to get common users in table 1, 2 and 3 that are not in table 4 and 5.
Can someone please help me :)
Tables
table1(userid,discount)
table2(userid,discount)
table3(userid,discount)
table4(userid,discount)
table5(userid,discount)
One way, left join on the table rows to omit:
select *
from table1 a
join table2 b on (a.userid = b.userid)
join table3 c on (a.userid = c.userid)
left join table4 d on (a.userid = d.userid)
left join table5 e on (a.userid = e.userid)
where d.userid is null and e.userid is null;
Getting the users common to tables 1, 2, 3 is easy -- just do an inner join. To get all of those users which are not in tables 4 or 5, you could test for their non-existence in these tables in the where clause.
select *
from table1
join table2 on table1.userid = table2.userid
join table3 on table1.userid = table3.userid
where
not exists (select * from table4 where table4.userid = table1.userid)
and not exists (select * from table5 where table5.userid = table1.userid)
Try this query
SELECT t1.userid, t2.userid, t2.userid, t4.userid, t5.userid
FROM table1 t1
LEFT JOIN table2 t2 ON (t2.userid = t1.userid)
LEFT JOIN table3 t3 ON (t3.userid = t1.userid)
LEFT JOIN table4 t4 ON (t4.userid = t1.userid)
LEFT JOIN table5 t5 ON (t5.userid = t1.userid)
GROUP BY t1.userid
HAVING t1.userid IS NOT NULL
AND t2.userid IS NOT NULL
AND t3.userid IS NOT NULL
AND t4.userid IS NULL
AND t5.userid IS NULL
SELECT *
FROM tableA a
JOIN tableB b ON (a.userid = b.userid)
JOIN tableC c ON (a.userid = c.userid)
LEFT JOIN tableD d ON (a.userid = d.userid)
LEFT JOIN tableE e ON (a.userid = e.userid)
WHERE d.userid IS NULL and e.userid IS NULL;

MySQL - replace LEFT JOIN inplace of NOT IN

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;