如何解决MySQL错误:在这种情况下为#1064?

时间:2019-06-22 17:06:49

标签: mysql sql

MySQL查询:

SELECT SUM(Target) AS target FROM salesmen WHERE `Area Code` IN (SELECT `Area Code` FROM distributors_areas WHERE Regional_Manager IN (SELECT `Name` FROM `regional managers` WHERE ID = 0) OR WHERE Territory_Manager IN (SELECT Name FROM `territory managers` WHERE ID = 'TM_02'))

运行此查询时,出现以下错误:#1064-您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册,以在“ WHERE Territory_Manager IN”(第1行的territory managers WHERE ID ='TM'中选择名称)附近使用正确的语法

1 个答案:

答案 0 :(得分:1)

SQL中没有or where。只是or

SELECT SUM(s.Target) AS target
FROM salesmen s
WHERE s.`Area Code` IN
           (SELECT da.`Area Code`
            FROM distributors_areas da
            WHERE da.Regional_Manager IN
                      (SELECT rm.`Name`
                       FROM `regional managers` rm
                       WHERE rm.ID = 0
                      ) OR 
                  da.Territory_Manager IN
                      (SELECT tm.Name
                       FROM `territory managers` tm
                       WHERE tm.ID = 'TM_02'
                      )
           );

我想我在前面的问题中提到过,您应该在查询中使用表别名。 我在这里重复这个建议!。当您有多个表引用时,应始终习惯于包含表别名并限定所有列名,以避免查询中常见的可避免的错误。