MySQL:使用Join时的语法错误?

时间:2011-10-10 16:19:04

标签: mysql join syntax-error

您是否发现此查询有任何问题?

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

已经工作了一段时间,但似乎无法正确使用它!

数据库如下所示:

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

 -request_data
   -request_id
   -key
   -value
编辑:哦,对,错误:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

WHERE位置错误。

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

您可能不需要GROUP BY因为WHERE确保只返回一个id,除非您以某种方式依赖隐藏列功能(您不应该是the results are undefined)。