在Database SQL中测试时查询错误

时间:2014-02-18 14:26:51

标签: mysql sql phpmyadmin

我有这个查询,显然它有问题吗? 我正在尝试与fices加入mems,因此我可以将ficeID以及mems的所有结果(这些查询单独运行)。我做错了什么?

  SELECT * 
    FROM mems 
   WHERE deleted <> -1 
ORDER BY sort_mems 
         LEFT JOIN SELECT ficeID 
                     FROM fices

结果:

 #1064 - 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 SELECT ficeID FROM offices LIMIT 0, 30' at line 1

2 个答案:

答案 0 :(得分:1)

  • JOIN之后您有ORDER BY个子句。您应该将其放在FROM
  • 我建议您定义LEFT JOIN
  • 的条件
  • 另外,我建议你用括号围绕临时表:

      SELECT m.*, t1.officeID
        FROM members m
             LEFT JOIN offices t1
                       ON m.memberID = t1.memberID
       WHERE m.deleted <> -1 
    ORDER BY m.sort_membername;
    

答案 1 :(得分:1)

是的,您的LEFT JOIN位于错误的位置(它应该在您的FROM子句之后,并且您似乎也错过了加入条件(ON部分,这告诉数据库这些表是如何相关的):

SELECT *
FROM mems m
LEFT JOIN fices f
  ON  m.??? = f.???
WHERE deleted <> -1 
ORDER BY sort_mems