sql count+join抛出语法错误,但在没有count的情况下可以正常工作

pcrecxhr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(531)

以下查询出现奇怪的语法错误:

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

但是没有 COUNT 很有魅力:

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

添加连接规范(比如inner、left等)没有帮助。
语法错误并没有具体说明错误所在,只是提供了某些代码:[42000][1064]
我该怎么修?

编辑:

在这里:

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

删除 COUNT 使它完全有效。真奇怪!

q43xntqr

q43xntqr1#

删除间距 COUNT 以及 (*) :

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

dbfiddle演示

编辑:

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

现在,我能补充一下吗?似乎使用count(distinct…)又开始抛出错误!
只需定义列名:

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

相关问题