qwerty
qwerty

Reputation: 5246

MySQL: Syntaxerror when using Join?

Can you find anything wrong with this query?

        SELECT * FROM requests 
        WHERE id = '".$id."' 
        LEFT JOIN request_data ON (requests.id = request_data.request_id) 
        GROUP BY requests.id 

Been workingon it for a while but can't seem to get it right!

The database looks like this:

 -requests
   -id
   -another column
   -and a third one

 -request_data
   -request_id
   -key
   -value

EDIT: Oh right, and the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN request_data ON (requests.id = request_data.request_id) GROUP BY ' at line 3

Any ideas?

Upvotes: 0

Views: 41

Answers (1)

Martin Smith
Martin Smith

Reputation: 453152

The WHERE is in the wrong place.

    SELECT *
    FROM requests 
    LEFT JOIN request_data ON (requests.id = request_data.request_id) 
    WHERE id = '".$id."' 

You probably don't need a GROUP BY either as the WHERE ensures there will only be one id returned unless in some way you are relying on the hidden columns functionality (which you shouldn't as the results are undefined).

Upvotes: 2

Related Questions