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;
Related
SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulmn
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulmn
LEFT JOIN table4 AS t4
ON profile_id = t4.some_column
I'm trying to use an alias (profile_id) inside my join condition. It fails. Is there a way to do this?
Column aliases defined in th SELECT clause cannot be used in the join conditions. This is because the FROM clause is evaluated before the SELECT clause.
If I followed you correctly, you probably want:
SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulm
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulm
LEFT JOIN table4 AS t4
ON IFNULL(t2.profile_id, t3.profile_id) = t4.some_column
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 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.
Are there any reasons why UNIONs shouldn't be used in the WHERE clause of update subqueries? Or for that matter, even normal select subqueries?
Is there a better way such a query to eliminate the UNION?
Note that for my case, the UNION will result in a fairly small number of records.
UPDATE mytable
set mytable.bla='xxx'
WHERE id IN (
SELECT id
FROM t1
INNER JOIN t2 ON t2.t1_id=t1.id
LEFT OUTER JOIN t3 ON t3.t1_id=t2.id
WHERE t2.id IN (1,2,3) AND t3.id IS NULL
UNION
SELECT id FROM t4
INNER JOIN t5 ON t5.id=t4.t5_id
LEFT OUTER JOIN t6 ON t6.t5_id=t5.id
WHERE t5.parent_id IN (1,2,3) AND t6.id IS NULL
);
Switching it to a join:-
UPDATE mytable
INNER JOIN
(
SELECT id
FROM t1
INNER JOIN t2 ON t2.t1_id=t1.id
LEFT OUTER JOIN t3 ON t3.t1_id=t2.id
WHERE t2.id IN (1,2,3)
AND t3.id IS NULL
UNION
SELECT id
FROM t4
INNER JOIN t5 ON t5.id=t4.t5_id
LEFT OUTER JOIN t6 ON t6.t5_id=t5.id
WHERE t5.parent_id IN (1,2,3)
AND t6.id IS NULL
) sub0
ON mytable.id = sub0.id
SET mytable.bla='xxx'
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