mysql - select if also is in other tables - mysql

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
)

Related

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

inner join between 2 tables - odd syntax?

see the following SQL..
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.user_id = t2.id, tables3 t3
WHERE t3.id = 999
what kind of join is there here? between t1 and t3?. I mean what is the comma all about?
A , in the FROM clause is a deprecated shorthand for CROSS JOIN
A better way to write the query is with an explicit CROSS JOIN:
SELECT t1.*
FROM table1 t1 INNER JOIN
table2 t2
ON t1.user_id = t2.id CROSS JOIN
tables3 t3
WHERE t3.id = 999 ;
As written, the query makes no sense. Assuming that t3.id = 999 is true and appears once, then this is equivalent to:
SELECT t1.*
FROM table1 t1 INNER JOIN
table2 t2
ON t1.user_id = t2.id;
If the value doesn't exist, then no rows will be returned.
SELECT t1.*
FROM table1 t1
JOIN table2 t2 ON t1.user_id = t2.id
JOIN tables3 t3 ON t2.id=t3.id
WHERE t3.id = 999

Selecting from multiple tables with LEFT JOIN

I have 3 tables
t1 (select these records)
-------------
id
offer_id
business_id
t2 (offer details)
-------------
id
offer_details
business_id
t3 (business details)
-------------
id
business_name
I need to select all records from t1 and add information from t2 and t3. Seems basic but I can't seem to be able to get it right -- must be the heat.
SELECT t2.offer_details, t3.business_name
FROM t2
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
should be
SELECT t2.offer_details, t3.business_name
FROM t1
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
Your lead table is t1 and the join should be based on this table
How about this
Select t2.offer_details, t3.business_name
From t1
Left Join t2 ON (t1.offer_id = t2.id)
Left Join t3 ON (t1.business_id = t3.id)
If you want all records from t1, add t1.* on your select part. Assuming that all IDs in t1 exists in the other 2 tables
SELECT
t1.*, t2.offer_details, t3.business_name
FROM
t1
JOIN t2 ON t2.id = t1.offer_id
JOIN t3 ON t3.id = t1.business_id
Modify to LEFT JOIN if the IDs in t1 may be missing in t2 or t3.

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

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;