2017-03-15 127 views
-2

我有以下表MySQL查詢圖書館管理系統

成員(memberNo,姓名,年齡),

書(ISBN,書名,作者,出版者,出版年,類別)

借(memberNo,ISBN,日期)

和我期待的答案或線索以下查詢

查找成員的瓦時的名字o不止一次借過同一本書。我一直在使用下列但它給我就列名「name」

SELECT member.name 
    FROM member 
    WHERE member.memberNo IN (SELECT borrowed.member_memberNo 
    FROM borrowed 
    WHERE borrowed.book_isbn IN(
    SELECT COUNT(borrowed.book_isbn) as 'countNo' 
    FROM borrowed 
    WHERE 'countNo' >1)); 

非常感謝

+0

詢問家庭作業幫助的問題必須包括您迄今爲止解決問題所做的工作摘要,以及您解決問題的難點描述。 –

+1

@Isaac Fife是正確的 - 但我心情很好,會給你一些提示:搜索GROUP BY和HAVING。祝你好運。 – user3741598

回答

0

您可以使用GROUP BYCOUNT來查找成員借用每本書的次數。

SELECT memberNo, isbn, COUNT(*) AS borrow_times 
FROM borrowed 
GROUP BY memberNo, isbn 

然後檢查borrow_times > 1找到誰借的同一本書不止一次的:

HAVING borrow_times > 1 

將這個變成一個子查詢,以便您可以用member表加入吧

SELECT DISTINCT m.name 
FROM member AS m 
JOIN (
    SELECT memberNo, isbn, COUNT(*) AS borrow_times 
    FROM borrowed 
    GROUP BY memberNo, isbn 
    HAVING borrow_times > 1 
) AS b ON m.memberNo = b.memberNo 
+0

現在明白了。非常感謝! – Beyg