为什么在嵌套查询中使用别名时出现语法错误?

时间:2019-01-04 00:26:41

标签: mysql sql

var car = [['mazda','red','at'],['honda','blue','at'],['toyota','green','mt']]
var mazda = [] //i want this array contain ['mazda','red','at']
var honda = [] //i want this array contain ['honda','blue','at']
var toyota = [] //i want this array contain ['toyota','green','mt']

19:21:06错误代码:1064。您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第7 0.000秒的行“ co”附近使用

谢谢!

2 个答案:

答案 0 :(得分:0)

IN()NOT IN()谓词必须具有带括号的值列表或子查询。您的谓词中没有任何内容。

此外,即使您有括号,也无法将列与类似的别名进行比较。在WHERE子句的上下文中,一列是标量,但别名是一组列。

我想您正在寻找没有订单的客户。这是两种获取方法:

SELECT c.Name
FROM customers AS c
WHERE c.Id NOT IN (SELECT CustomerId FROM orders);

或者:

SELECT c.Name
FROM customers AS c
LEFT OUTER JOIN orders AS o ON c.Id=o.CustomerId
WHERE o.CustomerId IS NULL;

答案 1 :(得分:0)

可能您想获得尚未下订单的客户的姓名
但不是将联接的查询放在NOT IN之后,而是将联接的查询放在FROM运算符之后。
如果是这种情况,请使用以下方法:

SELECT Name
FROM customers
WHERE Name NOT IN (
  SELECT c.Name
  FROM customers AS c
  INNER JOIN orders AS o
  ON c.Id=o.CustomerId
)