SELECT paid,schoolsup,famsup,nursery from student_mat GROUP BY paid,schoolsup,famsup,nursery根据这个分组 根绝字段中有4个/3个/2个yes的进行计数
时间: 2023-09-10 11:09:05 浏览: 142
可以使用以下SQL语句来实现对同时有4个/3个/2个yes的进行计数:
同时有4个yes:
```
SELECT COUNT(*) as count FROM (
SELECT paid,schoolsup,famsup,nursery
FROM student_mat
WHERE paid='yes' AND schoolsup='yes' AND famsup='yes' AND nursery='yes'
GROUP BY paid,schoolsup,famsup,nursery
) as t;
```
同时有3个yes:
```
SELECT COUNT(*) as count FROM (
SELECT paid,schoolsup,famsup,nursery
FROM student_mat
WHERE (paid='yes' AND schoolsup='yes' AND famsup='yes') OR
(paid='yes' AND schoolsup='yes' AND nursery='yes') OR
(paid='yes' AND famsup='yes' AND nursery='yes') OR
(schoolsup='yes' AND famsup='yes' AND nursery='yes')
GROUP BY paid,schoolsup,famsup,nursery
) as t;
```
同时有2个yes:
```
SELECT COUNT(*) as count FROM (
SELECT paid,schoolsup,famsup,nursery
FROM student_mat
WHERE (paid='yes' AND schoolsup='yes') OR
(paid='yes' AND famsup='yes') OR
(paid='yes' AND nursery='yes') OR
(schoolsup='yes' AND famsup='yes') OR
(schoolsup='yes' AND nursery='yes') OR
(famsup='yes' AND nursery='yes')
GROUP BY paid,schoolsup,famsup,nursery
) as t;
```
这些语句将对每个满足条件的分组进行计数,答案将以 `count` 字段的形式返回。
阅读全文