I am trying to find rows if the second query that do not exist in the first. But I get the 'You have error in your SQL syntax near a right join' error.
(SELECT t1.id AS id, t2.id, t3.id
FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table3 t3 ON t3.id = t2.id
) a
RIGHT JOIN
(SELECT id
FROM table4
WHERE col1 IS NOT NULL AND col2 IN (1, 2)) b
ON a.id = b.id
What do I do wrong in this query? Thank you.
In join queries, you must put all your columns in the first select statement. The second select statement in your query is invalid.
SELECT
t1.id as id, t2.id, t3.id, b.id
FROM
(table1 t1
inner join table2 t2 on t2.id=t1.id
inner join table3 t3 on t3.id=t2.id)
right join table4 b on t1.id = b.id
WHERE b.col1 IS NOT NULL AND b.col2 IN (1, 2))
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
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
I have 2 tables and result as shown in the image below: MySQL DB
What would be best way to join the two tables so we get the result as shown above.
SELECT * FROM (SELECT id, desc FROM table2) as T1
LEFT JOIN (SELECT * FROM table1) as T2 ON T1.id = T2.id
I guess my SQL is not working.
You can use a LEFT JOIN with COALESCE:
SELECT t1.id, COALESCE(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
Use a left join with coalesce to prioritize table 2's values if they are present, but fallback on table 1's values if not.
select t1.id,
coalesce(t2.desc, t1.desc) as desc,
t1.d1, t1.d2
from table1 t1
left join table2 t2
on t2.id = t1.id
order by t1.id
You can use ifnull:
SELECT t1.id, ifnull(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1
LEFT JOIN table2 as T2 ON T1.id = T2.id
coalesce or case .. when is also possible. All together with the left join
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;
select t1.table1 from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
Above is the mysql query i am firing. Wanted some data from the subquery tables also.
For example say columnxy from table t2.
query that fails
select t1.table1,t2.columnxy from table1 as t1
where t1.column1
in
(
select t2.column2 from table2 as t2
join
table3 as t3 on t2.column1=t3.column1
where t3.columnx=5
);
If i add them with select of the outer query gives error "unknown column" which does make sense.
Is the right way or should rewrite query with joins?
Rewrite the query with joins:
SELECT t1.table1, t.columnxy
FROM table1 AS t1 JOIN (
SELECT t2.column2, t2.columnxy
FROM table2 AS t2 JOIN table3 AS t3 USING (column1)
WHERE t3.columnx = 5
) t ON t1.column1 = t.column2
Or:
SELECT t1.table1, t2.columnxy
FROM table1 AS t1
JOIN table2 AS t2 ON t1.column1 = t2.column2
JOIN table3 AS t3 ON t2.column1 = t3.column1
WHERE t3.columnx = 5
The t2 is not available at that point. You should use a join for this. Using t1.column1=t2.column2 should do it.