在 library 数据库中,根据任务要求完成 SQL 语句的编写。
时间: 2024-12-21 17:30:58 浏览: 16
在 Library 数据库中,如果需要完成特定任务的 SQL 语句,可能会涉及各种查询操作。这里假设我们有基本的数据库结构,如书籍表(Books)、作者表(Authors)和借阅记录表(Borrowings)。以下是几个常见的任务对应的 SQL 示例:
1. **查找所有书籍**:
```sql
SELECT * FROM Books;
```
2. **检索指定作者的所有作品**:
```sql
SELECT * FROM Books WHERE author_id = (SELECT id FROM Authors WHERE name = '张三');
```
3. **统计某本书的借阅次数**:
```sql
SELECT COUNT(*) FROM Borrowings WHERE book_id = (SELECT id FROM Books WHERE title = '《红楼梦》');
```
4. **获取最近一周内借出的书籍列表**:
```sql
SELECT B.* FROM Books B JOIN Borrowings BA ON B.id = BA.book_id
WHERE BA.borrow_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY);
```
5. **查询未归还的图书**:
```sql
SELECT B.title FROM Books B LEFT JOIN Borrowings BA ON B.id = BA.book_id
WHERE BA.return_date IS NULL;
```
6. **更新借阅状态**:
```sql
UPDATE Borrowings SET return_date = '2023-03-01' WHERE borrower_id = 123 AND book_id = 456;
```
阅读全文