2012-02-02 66 views
0

我有兩個表:「members_info」和「members_lesson」。Mysql查詢,看看一個成員是否已經完成了課程

讓我們在members_info我已經等領域member_id,FIRST_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

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)

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 
+0

自6 <7它會工作,但在它將更通用的使用「不存在」 – alfasin 2012-02-02 19:17:33

相關問題