SQL count + join 抛出语法错误,但没有计数也能正常工作
SQL count + join throws syntax error, but works ok without count
我在以下查询中遇到奇怪的语法错误:
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 等 JOIN 规范 - 没有帮助。
The syntax error doesn't specify exactly what's wrong, just provides certain codes: [42000][1064]
我该如何解决?
编辑:
这里是:
SELECT COUNT(customers.*)
FROM customers
JOIN customer_behaviour ON customers.id = customer_behaviour.customer_id
删除COUNT
使其完全有效。这真的很奇怪!
删除 COUNT
和 (*)
之间的 space:
SELECT COUNT(*) FROM a JOIN b ON a.id = b.a_id
-- COUNT (*)
编辑:
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
Now, can I add DISTINCT to it? Seems like using COUNT (DISTINCT ...) starts throwing the error again!
定义列名即可:
SELECT COUNT(DISTINCT table_name.column_name)
FROM customers
JOIN customer_behaviour
ON customers.id = customer_behaviour.customer_id
我在以下查询中遇到奇怪的语法错误:
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 等 JOIN 规范 - 没有帮助。
The syntax error doesn't specify exactly what's wrong, just provides certain codes: [42000][1064]
我该如何解决?
编辑:
这里是:
SELECT COUNT(customers.*)
FROM customers
JOIN customer_behaviour ON customers.id = customer_behaviour.customer_id
删除COUNT
使其完全有效。这真的很奇怪!
删除 COUNT
和 (*)
之间的 space:
SELECT COUNT(*) FROM a JOIN b ON a.id = b.a_id
-- COUNT (*)
编辑:
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
Now, can I add DISTINCT to it? Seems like using COUNT (DISTINCT ...) starts throwing the error again!
定义列名即可:
SELECT COUNT(DISTINCT table_name.column_name)
FROM customers
JOIN customer_behaviour
ON customers.id = customer_behaviour.customer_id