{ "data": { "question": { "questionId": "3910", "questionFrontendId": "3570", "categoryTitle": "Database", "boundTopicId": 3691093, "title": "Find Books with No Available Copies", "titleSlug": "find-books-with-no-available-copies", "content": "
Table: library_books
\n+------------------+---------+\n| Column Name | Type |\n+------------------+---------+\n| book_id | int |\n| title | varchar |\n| author | varchar |\n| genre | varchar |\n| publication_year | int |\n| total_copies | int |\n+------------------+---------+\nbook_id is the unique identifier for this table.\nEach row contains information about a book in the library, including the total number of copies owned by the library.\n\n\n
Table: borrowing_records
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| record_id | int |\n| book_id | int |\n| borrower_name | varchar |\n| borrow_date | date |\n| return_date | date |\n+---------------+---------+\nrecord_id is the unique identifier for this table.\nEach row represents a borrowing transaction and return_date is NULL if the book is currently borrowed and hasn't been returned yet.\n\n\n
Write a solution to find all books that are currently borrowed (not returned) and have zero copies available in the library.
\n\nreturn_date
Return the result table ordered by current borrowers in descending order, then by book title in ascending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nlibrary_books table:
\n\n\n+---------+------------------------+------------------+----------+------------------+--------------+\n| book_id | title | author | genre | publication_year | total_copies |\n+---------+------------------------+------------------+----------+------------------+--------------+\n| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |\n| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 |\n| 3 | 1984 | George Orwell | Dystopian| 1949 | 1 |\n| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 |\n| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 |\n| 6 | Brave New World | Aldous Huxley | Dystopian| 1932 | 4 |\n+---------+------------------------+------------------+----------+------------------+--------------+\n\n\n
borrowing_records table:
\n\n\n+-----------+---------+---------------+-------------+-------------+\n| record_id | book_id | borrower_name | borrow_date | return_date |\n+-----------+---------+---------------+-------------+-------------+\n| 1 | 1 | Alice Smith | 2024-01-15 | NULL |\n| 2 | 1 | Bob Johnson | 2024-01-20 | NULL |\n| 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 |\n| 4 | 3 | David Brown | 2024-02-01 | NULL |\n| 5 | 4 | Emma Wilson | 2024-01-05 | NULL |\n| 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 |\n| 7 | 1 | Grace Miller | 2024-02-05 | NULL |\n| 8 | 6 | Henry Taylor | 2024-01-12 | NULL |\n| 9 | 2 | Ivan Clark | 2024-02-12 | NULL |\n| 10 | 2 | Jane Adams | 2024-02-15 | NULL |\n+-----------+---------+---------------+-------------+-------------+\n\n\n
Output:
\n\n\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| book_id | title | author | genre | publication_year | current_borrowers |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | \n| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n\n\n
Explanation:
\n\nOutput table is ordered by current_borrowers in descending order, then by book_title in ascending order.
\n表:library_books
\n+------------------+---------+\n| Column Name | Type |\n+------------------+---------+\n| book_id | int |\n| title | varchar |\n| author | varchar |\n| genre | varchar |\n| publication_year | int |\n| total_copies | int |\n+------------------+---------+\nbook_id 是这张表的唯一主键。\n每一行包含图书馆中一本书的信息,包括图书馆拥有的副本总数。\n\n\n
表:borrowing_records
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| record_id | int |\n| book_id | int |\n| borrower_name | varchar |\n| borrow_date | date |\n| return_date | date |\n+---------------+---------+\nrecord_id 是这张表的唯一主键。\n每一行代表一笔借阅交易并且如果这本书目前被借出并且还没有被归还,return_date 为 NULL。\n\n\n
编写一个解决方案以找到 所有 当前被借出(未归还) 且图书馆中 无可用副本 的书籍。
\n\nreturn_date
为 NULL,那么这本书被认为 当前是借出的。返回结果表按当前借阅者数量 降序 排列,然后按书名 升序 排列。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nlibrary_books 表:
\n\n\n+---------+------------------------+------------------+----------+------------------+--------------+\n| book_id | title | author | genre | publication_year | total_copies |\n+---------+------------------------+------------------+----------+------------------+--------------+\n| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 |\n| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 1960 | 3 |\n| 3 | 1984 | George Orwell | Dystopian| 1949 | 1 |\n| 4 | Pride and Prejudice | Jane Austen | Romance | 1813 | 2 |\n| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 1951 | 1 |\n| 6 | Brave New World | Aldous Huxley | Dystopian| 1932 | 4 |\n+---------+------------------------+------------------+----------+------------------+--------------+\n\n\n
borrowing_records 表:
\n\n\n+-----------+---------+---------------+-------------+-------------+\n| record_id | book_id | borrower_name | borrow_date | return_date |\n+-----------+---------+---------------+-------------+-------------+\n| 1 | 1 | Alice Smith | 2024-01-15 | NULL |\n| 2 | 1 | Bob Johnson | 2024-01-20 | NULL |\n| 3 | 2 | Carol White | 2024-01-10 | 2024-01-25 |\n| 4 | 3 | David Brown | 2024-02-01 | NULL |\n| 5 | 4 | Emma Wilson | 2024-01-05 | NULL |\n| 6 | 5 | Frank Davis | 2024-01-18 | 2024-02-10 |\n| 7 | 1 | Grace Miller | 2024-02-05 | NULL |\n| 8 | 6 | Henry Taylor | 2024-01-12 | NULL |\n| 9 | 2 | Ivan Clark | 2024-02-12 | NULL |\n| 10 | 2 | Jane Adams | 2024-02-15 | NULL |\n+-----------+---------+---------------+-------------+-------------+\n\n\n
输出:
\n\n\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| book_id | title | author | genre | publication_year | current_borrowers |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n| 1 | The Great Gatsby | F. Scott | Fiction | 1925 | 3 | \n| 3 | 1984 | George Orwell | Dystopian | 1949 | 1 |\n+---------+------------------+---------------+-----------+------------------+-------------------+\n\n\n
解释:
\n\n输出表以 current_borrowers 降序排序,然后以 book_title 升序排序。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"library_books\":[\"book_id\",\"title\",\"author\",\"genre\",\"publication_year\",\"total_copies\"],\"borrowing_records\":[\"record_id\",\"book_id\",\"borrower_name\",\"borrow_date\",\"return_date\"]},\"rows\":{\"library_books\":[[1,\"The Great Gatsby\",\"F. Scott\",\"Fiction\",1925,3],[2,\"To Kill a Mockingbird\",\"Harper Lee\",\"Fiction\",1960,3],[3,\"1984\",\"George Orwell\",\"Dystopian\",1949,1],[4,\"Pride and Prejudice\",\"Jane Austen\",\"Romance\",1813,2],[5,\"The Catcher in the Rye\",\"J.D. Salinger\",\"Fiction\",1951,1],[6,\"Brave New World\",\"Aldous Huxley\",\"Dystopian\",1932,4]],\"borrowing_records\":[[1,1,\"Alice Smith\",\"2024-01-15\",null],[2,1,\"Bob Johnson\",\"2024-01-20\",null],[3,2,\"Carol White\",\"2024-01-10\",\"2024-01-25\"],[4,3,\"David Brown\",\"2024-02-01\",null],[5,4,\"Emma Wilson\",\"2024-01-05\",null],[6,5,\"Frank Davis\",\"2024-01-18\",\"2024-02-10\"],[7,1,\"Grace Miller\",\"2024-02-05\",null],[8,6,\"Henry Taylor\",\"2024-01-12\",null],[9,2,\"Ivan Clark\",\"2024-02-12\",null],[10,2,\"Jane Adams\",\"2024-02-15\",null]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"