1

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.

4

1 回答 1

1

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))
于 2013-06-13T19:53:42.930 回答