user2605381
user2605381

Reputation: 81

Why is mysql saying there is a syntax error?

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

Answers (5)

VoidA313
VoidA313

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

Ravi
Ravi

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

Adriaan Stander
Adriaan Stander

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

hkutluay
hkutluay

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

GolezTrol
GolezTrol

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

Related Questions