Reputation: 81
I'm pretty new to the world of outer joins, and I'm trying to get used to them, but I have no idea why mysql is saying the syntax is incorrect. Does anyone care to give some insight?
SELECT * FROM user_courses, course_updates
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
Upvotes: 0
Views: 72
Reputation: 546
Yes your syntax is wrong, in your case use WHERE clause at the end of the statement as follows:
SELECT *
FROM user_courses uc
INNER JOIN course_updates cu ON cu.crn = uc.crn
LEFT OUTER JOIN followers f ON f.followee = cu.USER_ID
WHERE uc.USER_ID = 1
Also make sure f.followee type and cu.USER_ID are the same.
Hope this would help you!!!
Upvotes: 0
Reputation: 31407
Try This..
SELECT * FROM user_courses INNER JOIN course_updates ON user_courses.crn = course_updates.crn LEFT JOIN followers ON followers.followee = course_updates.user_id WHERE user_courses.user_id = 1
Since, WHERE
clause will be used after JOIN
Upvotes: 0
Reputation: 166396
Because there is a syntax error.
Try
SELECT *
FROM user_courses LEFT JOIN
course_updates ON user_courses.crn = course_updates.crn
AND user_courses.user_id = 1 LEFT JOIN
followers ON (followers.followee = course_updates.user_id)
From enter link description here
the FROM table_references
comes before the WHERE where_condition
Also maybe look at JOIN Syntax
Upvotes: 0
Reputation: 6944
Use like this
SELECT * FROM user_courses, course_updates
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
Upvotes: 1
Reputation: 116110
The LEFT JOIN
clause should become before the WHERE
clause.
SELECT * FROM user_courses, course_updates
LEFT JOIN followers ON (followers.followee = course_updates.user_id)
WHERE user_courses.crn = course_updates.crn AND user_courses.user_id = 1
By the way, you can also use `INNER JOIN for you other tables, so you don't have two types of syntax:
SELECT * FROM user_courses
INNER JOIN course_updates on user_courses.crn = course_updates.crn
LEFT JOIN followers ON followers.followee = course_updates.user_id
WHERE user_courses.user_id = 1
Note that I have omitted the parentheses around the ON
condition, which is perfectly valid. Also, you can see that using INNER JOIN
, you can specify the join conditions in the join itself, leaving the WHERE
clause solely for filtering. I think this results in better readability of your query.
Upvotes: 3