简体   繁体   中英

Syntax error on LEFT OUTER JOIN

I have the following MySQL query:

SELECT w1.Id
FROM Wills w1 LEFT OUTER JOIN Wills w2
WHERE w1.Id = w2.Id + 1
AND w1.Tmp > w2.Tmp

With the query, I want to compare rows with adjacent Id s. I want to select those that have a greater Tmp field than their predecessor.

However, I get the following error:

Runtime Error Message:
Line 3: SyntaxError: near 'WHERE w1.Id = w2.Id + 1
AND w1.Tmp > w2.Tmp'

I don't have much experience with SQL and MySQL. Did I do the join incorrectly?

You miss the ON clause:

SELECT w1.Id
FROM Wills w1 LEFT OUTER JOIN Wills w2 ON w1.Id = w2.Id + 1
and  w1.Tmp > w2.Tmp

You need to put the conditions in the ON clause rather than a WHERE clause:

SELECT w1.Id
FROM Wills w1 LEFT OUTER JOIN 
     Wills w2
     ON w1.Id = w2.Id + 1 AND w1.Tmp > w2.Tmp;

If either condition is in the WHERE , then the LEFT JOIN will be turned into an INNER JOIN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM