1

我有一个选择,它会显示标题、bookcopyid 和名称。

select 
    books.title, borrow.bookcopiesid, users.name, usersid,library_locations.name, checkout_date, return_date, due_date 
FROM 
    books,  borrow,users, library_locations, userlib 
WHERE 
    library_locations.id = userlib.libid 
AND 
    userlib.userid = users.id 
AND 
    borrow.bookcopiesid = books.bookid 
AND 
    borrow.usersid = users.id and return_date is not null ;

我怎么能得到类似的东西

SELECT title, COUNT(*) as count 
FROM (
    SELECT books.title, borrow.bookcopiesid, users.name, usersid,library_locations.name, checkout_date, return_date, due_date 
    FROM books,  borrow,users, library_locations, userlib 
    WHERE library_locations.id = userlib.libid and userlib.userid = users.id and borrow.bookcopiesid = books.bookid and borrow.usersid = users.id and return_date is not null) 
GROUP BY title 
ORDER BY count DESC); 

去工作。

我正在尝试显示每个名称的标题数

4

1 回答 1

1

我想这就是你要找的吗?

SELECT
    books.title,
    COUNT(*) as count
FROM
    books, 
    borrow,
    users, 
    library_locations,
    userlib 
WHERE 
    library_locations.id = userlib.libid 
    AND userlib.userid = users.id 
    AND borrow.bookcopiesid = books.bookid 
    AND borrow.usersid = users.id 
    AND return_date is not null
GROUP BY books.title 
ORDER BY COUNT(*) DESC;

不需要子查询;您只需要限定SELECTandGROUP BY子句中的列(就像您在WHERE子句中所做的那样)。

另外,return_date需要有资格......但我不知道来自哪个表,所以你可以自己添加。

于 2012-11-30T01:03:23.157 回答