我有2个表:“members_info”和“members_lesson”。
假设在members_info中我有字段member_id,first_name,last_name 在members_lesson中,我有members_lesson_id,lesson_id,member_id
我想创建一个查询,在那里我可以看到已经完成一个课程但没有完成另一个课程的成员。我试过这个问题:
SELECT members_info.member_id
FROM members_info
JOIN member_lessons ON members_info.member_id = member_lessons.member_id
WHERE member_lessons.lesson_id =6 AND member_lessons.lesson_id !=7
但该查询无效。当我只检查该成员是否已完成课程时,它可以工作,但如果我想查看该成员是否还没有完成另一个成员,它就不起作用。有任何想法吗?谢谢!
编辑 -
我试过了:
SELECT members_info
FROM members_info
WHERE member_id IN (SELECT member_id FROM member_lessons WHERE lesson_id = 6)
AND member_id NOT IN (SELECT member_id FROM member_lessons WHERE lesson_id = 7)
它确实有效,但我不确定性能。这样好吗?谢谢!
答案 0 :(得分:1)
试试这个:
SELECT member_id, max(lesson_id)
FROM member_lessons ml
group by member_id
WHERE lesson_id in (6, 7)
having count(distinct lesson_id) = 1
and max(lesson_id) = 6
答案 1 :(得分:0)
SELECT members_info FROM members_info WHERE member_id IN(SELECT member_id FROM member_lessons WHERE lesson_id = 6) AND member_id NOT IN(SELECT member_id FROM member_lessons WHERE lesson_id = 7)