库管理系统的mysql查询

时间:2017-03-15 20:37:46

标签: mysql sql database

我有下表

成员(memberNo,姓名,年龄),

书(isbn,title,author,publisher,year,category)

借(memberNo,isbn,date)

我正在寻找以下查询的答案或线索

查找多次借用同一本书的会员姓名。

我一直在使用以下内容,但它只给我一个名称'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));

非常感谢!

1 个答案:

答案 0 :(得分: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