[英]MySQL how to select records of a field doesn't have all required values
In MySQL, how to select the records with a specific field doesn't cover all required values? 在MySQL中,如何选择具有特定字段的记录不能覆盖所有必需的值?
for example, in the following records, some students finished continuous numbered assignments, like s1 finished from 1 to 5, s4 finished from 1 to 7, but s1 and s4 had different MAX NUMBER of assignments. 例如,在以下记录中,一些学生完成了连续编号的作业,例如s1从1到5完成,s4从1到7完成,但是s1和s4具有不同的MAX NUMBER作业。 While some students finished NON-CONTINUOUS numbered assignments, eg s2 and s3.
一些学生完成了不连续的编号作业,例如s2和s3。 how to select the students who have finished CONTINUOUS NUMBERED assignments, regardless the max number?
如何选择完成连续编号作业的学生,而不管最大人数如何?
[[More condition, sorry!]] I need one more column and condition to meet my real work, for example, "failed" column, where 1 means failed, 0 means passed. [[更多条件,对不起!]]我还需要一栏和条件才能满足我的实际工作,例如,“ failed”列,其中1表示失败,0表示通过。
Together with the original condition, I also want to exclude those students who have failed one or more assignments, that is, failed
must be 0. 连同原始条件,我还想排除那些未完成一项或多项任务(即必须
failed
为0)的学生。
In the following records with failed
column added, I only want to select student 1, because student 4 has failed assignment (although his assignments number are also continuous) 在以下添加了
failed
列的记录中,我只想选择学生1,因为学生4的作业分配失败(尽管他的作业编号也是连续的)
id student_id assignment_done failed
1 s1 1 0
2 s1 2 0
3 s1 3 0
4 s1 4 0
5 s1 5 0
6 s2 2 0
7 s2 4 0
8 s3 1 0
9 s3 5 0
10 s4 1 0
11 s4 2 0
12 s4 3 0
13 s4 4 0
14 s4 5 0
15 s4 6 0
16 s4 7 1
Select student_id from STUDENT group by student_id having count( assignment_done) = max(assignment_done);
+------------+
| student_id |
+------------+
| s1 |
| s4 |
+------------+
2 rows in set (0.00 sec)
Try this: 尝试这个:
SELECT student_id,
COUNT(CASE failed
WHEN '0' THEN 1
ELSE NULL
END) as failedCount
FROM #yourTableName(e.g, students)
GROUP BY student_id
HAVING COUNT(assignment_done) = MAX(assignment_done)
AND
MAX(assignment_done)=failedCount;
In a simple way you can use below sql query 您可以通过以下简单方式使用以下sql查询
SELECT distinct student_id FROM Students A
WHERE A.student_id in (
SELECT S.student_id
FROM Students S
GROUP BY S.student_id
HAVING COUNT(*) =5
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.