SQL count + join会引发语法错误,但无需计数即可正常工作

时间:2018-06-09 17:08:00

标签: mysql sql mysql-error-1064

我通过以下查询得到了奇怪的语法错误:

for value in self.gaugeDict.values():
    self.w.create_circle_arc(
        value[0],
        value[1],
        value[2],
        fill="black",
        outline="",
        start=180,
        end=0
)

但是在没有SELECT COUNT (*) FROM a JOIN b ON a.id = b.a_id 的情况下查询就像一个魅力:

COUNT

添加INNER,LEFT等JOIN规范并没有帮助。

  

语法错误并没有准确指出错误,只提供了某些代码:[42000] [1064]

我该如何解决?

编辑:

这是:

SELECT * 
FROM a 
JOIN b ON a.id = b.a_id

删除SELECT COUNT(customers.*) FROM customers JOIN customer_behaviour ON customers.id = customer_behaviour.customer_id 会使其完全有效。这太奇怪了!

1 个答案:

答案 0 :(得分:1)

删除COUNT(*)之间的空格:

SELECT COUNT(*) FROM a JOIN b ON a.id = b.a_id 
    -- COUNT (*)

<强> DBFiddle Demo

编辑:

SELECT COUNT(customers.*) 
FROM customers 
JOIN customer_behaviour 
  ON customers.id = customer_behaviour.customer_id

-- should be    
SELECT COUNT(*) 
FROM customers 
JOIN customer_behaviour 
  ON customers.id = customer_behaviour.customer_id

-- or
SELECT COUNT(customers.id) 
FROM customers 
JOIN customer_behaviour 
  ON customers.id = customer_behaviour.customer_id

编辑2

  

现在,我可以添加DISTINCT吗?好像使用COUNT(DISTINCT ...)开始再次抛出错误!

只需定义列名称:

SELECT COUNT(DISTINCT table_name.column_name) 
FROM customers 
JOIN customer_behaviour 
  ON customers.id = customer_behaviour.customer_id