LEFT JOIN和WHERE导致错误

时间:2011-06-29 19:55:49

标签: php mysql sql mysql-error-1064

我有一个嵌套的mysql_query。

$resultSub = mysql_query("SELECT * 
                            FROM ensembles 
                           WHERE en_name = $name 
                       LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID 
                       LEFT JOIN students on ensembles.en_stu = students.s_ID 
                       LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID 
                        ORDER BY $sort $orderBy");

在没有WHERE子句的情况下查询工作正常,我认为这可能会过滤掉LEFT JOIN命令的行,但事实并非如此。

2 个答案:

答案 0 :(得分:9)

WHERE子句应放在LEFT JOIN之后:

$resultSub = mysql_query("SELECT * 
                            FROM ensembles 
                       LEFT JOIN ensemble_names on ensembles.en_name = ensemble_names.en_nm_ID 
                       LEFT JOIN students on ensembles.en_stu = students.s_ID 
                       LEFT JOIN part_names on ensembles.en_part = part_names.p_nm_ID 
                           WHERE en_name = $name 
                        ORDER BY $sort $orderBy");

答案 1 :(得分:0)

好吧,你把WHERE子句放在了错误的地方。

阅读the documentation