【发布时间】:2021-02-01 23:48:04
【问题描述】:
SELECT COUNT(student_id) AS count
FROM student_details
WHERE STATUS='REGISTER'
AND student_id NOT IN (
SELECT student_id FROM student_details WHERE STATUS='CANCEL'
)
AND registered_on< '2020-10-15 00:00:00'
我试过 NOT EXIST 但没有得到预期的结果
SELECT COUNT(DISTINCT S.student_id) AS ren
FROM student_details S
WHERE
S.status = 'REGISTER'
AND S.registered_on < '2020-10-15 00:00:00'
AND NOT EXISTS (
SELECT 1
FROM student_details S1
WHERE S.student_id = S1.student_id AND S1.status = 'CANCEL'
)
student_id重复条目不能做索引,status是有效条目,表有大量数据,需要减少执行时间。
【问题讨论】:
-
索引呢?
-
您可以在问题中添加 DESCRIBE 吗?
-
请编辑问题并添加
show create table student_details和show create table subscription和explain SELECT COUNT(...rest of your original query的输出(不是describe,它没有足够的帮助。)
标签: mysql sql count subquery mysql-workbench