我有一个包含列id,first_name,last_name,email,gender,department和car的表。我可以看到这个车的数量:
SELECT car, COUNT(car) FROM Workers GROUP BY car;
现在我试图查看仅由男性或女性拥有的汽车数量。我试过这个:
SELECT car, COUNT(car) FROM Workers GROUP BY car WHERE gender = 'Male';
但是它给出了语法错误。我该如何解决这个问题?
答案 0 :(得分:6)
Where
子句应该在group by
子句之前:
SELECT car, COUNT(car)
FROM Workers
WHERE gender = 'Male'
GROUP BY car;
因为你不想在记录之后想要记录级别的过滤。
我试图查看仅由男性或女性拥有的汽车数量。
所以,where
子句应该
WHERE gender IN ('Male', 'Female');
答案 1 :(得分:2)
如果您想在一个请求中做男性和女性,您可以这样做:
SELECT car, gender, COUNT(car)
FROM Workers
GROUP BY car, gender;
答案 2 :(得分:1)
试试这个:
SELECT gender, COUNT(car) FROM Workers GROUP BY gender ;
答案 3 :(得分:1)
很简单。
请写 group by
SELECT car, COUNT(car) FROM Workers
WHERE gender = 'Male' GROUP BY car