如何调试"您的SQL语法错误"在MySQL?

时间:2015-03-09 17:50:23

标签: mysql sql database syntax-error

SELECT loyalty_card.card_ID, Customer.first_name,customer.last_name, loyalty_card.points
FROM loyalty_card
INNER JOIN customer
ON loyalty_card.Customer_ID=Customer.Customer_ID;
having count(loyalty_card.points) > 100

我一直在尝试完成查询,但收到错误消息。

#1064 - You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax 
to use near 'having count(loyalty_card.points) > 100' at line 1

有人可以帮忙吗?我正在使用phpMyAdmin和MySQL。

2 个答案:

答案 0 :(得分:1)

为了使用'有条款'使用汇总功能,你需要按照'分组。您的查询中的子句。

语法如下:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;

因此;您的查询变为:

Select l1.card_ID, c1.first_name, c1.last_name, l1.points 
FROM loyalty_card l1
INNER JOIN customer c1 
ON l1.Customer_ID = c1.Customer_ID
group by l1.card_ID
having count(l1.points) > 100

答案 1 :(得分:0)

试试这个:

SELECT loyalty_card.card_ID, Customer.first_name,customer.last_name, loyalty_card.points 
FROM loyalty_card INNER JOIN customer ON loyalty_card.Customer_ID = Customer.Customer_ID
group by loyalty_card.card_ID
having count(loyalty_card.points) > 100