Mysql多连接语法错误

时间:2013-01-22 12:00:14

标签: mysql sql

我是mysql的新手,我一直遇到这个查询的问题。

考虑这个问题:

SELECT a.group_message_id, a.scheduled_date, b.message, c.phone_number, d.group_name
    FROM schedule AS a
    JOIN group_message AS b ON a.group_message_id = b.id,
    JOIN phonenumber AS c ON c.id = a.phonenumber_id,
    JOIN group_table AS d ON d.group_id = a.group_id
    WHERE a.status = 'unsent'

此错误消息:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'JOIN phonenumber AS c ON c.id = a.phonenumber_id,JOIN   第4行的group_table AS d'

我需要帮助解决该声明的实际错误。如果你能告诉我其他和“正确”的做法,我也会非常感激。

最后,如果没有足够的时间,是否有一个有效的资源,我至少可以从mysql的基础知识中学习。请分享。

2 个答案:

答案 0 :(得分:5)

删除连接语句上的逗号

JOIN group_message AS b ON a.group_message_id = b.id, -- <<== HERE
JOIN phonenumber AS c ON c.id = a.phonenumber_id,     -- <<== HERE
JOIN group_table AS d ON d.group_id = a.group_id

最终查询,

SELECT  a.group_message_id, 
        a.scheduled_date, 
        b.message, 
        c.phone_number, 
        d.group_name
FROM    schedule AS a
        JOIN group_message AS b 
            ON a.group_message_id = b.id
        JOIN phonenumber AS c 
            ON c.id = a.phonenumber_id
        JOIN group_table AS d 
            ON d.group_id = a.group_id
WHERE   a.status = 'unsent'

答案 1 :(得分:1)

你不应该使用逗号

只需检查语法here

示例语法

SELECT  column_list FROM  table1 JOIN table2 ON condition JOIN table3 ON condition WHERE condition