我有一张有总统名字的桌子,我想算一下有多少(不同的总统)服务超过4年。
所以我编写了以下查询,但它不起作用。错误是:
Invalid query: You have an error in your SQL syntax, near '*) FROM (SELECT DISTINCT P1.firstName, P1.lastName FROM Presidents P1 ' at line 1
查询是:
SELECT COUNT (*)
FROM (SELECT DISTINCT P1.firstName, P1.lastName
FROM Presidents P1
WHERE (P1.endTerm - P1.beginTerm) > 4) M;
有人看到语法错误吗?
谢谢!
答案 0 :(得分:2)
只需移除COUNT
和(*)
甚至更好地将查询重写为:
SELECT COUNT(DISTINCT P1.firstName, P1.lastName)
FROM Presidents P1
WHERE (P1.endTerm - P1.beginTerm) > 4
答案 1 :(得分:2)
删除COUNT后的空格。 MySQL不喜欢那个
SELECT COUNT(*)
FROM (SELECT DISTINCT P1.firstName, P1.lastName
....
另外,为什么不呢? MySQL允许您直接计算不同的对
SELECT COUNT(DISTINCT P1.firstName, P1.lastName)
FROM Presidents P1
WHERE (P1.endTerm - P1.beginTerm) > 4