读者姓名在表一,借阅记录在表二,查询借阅了一下1005这本图书的所有读者的姓名(使用exists)
时间: 2024-03-17 13:45:26 浏览: 65
假设表一为 "readers",包含读者的姓名和 ID,表二为 "borrows",包含借阅记录的书籍 ID 和读者 ID,则可以使用 EXISTS 子查询来查询借阅了书籍 ID 为 1005 的所有读者的姓名:
```
SELECT name
FROM readers
WHERE EXISTS (
SELECT *
FROM borrows
WHERE readers.id = borrows.reader_id AND borrows.book_id = 1005
);
```
该语句使用了 EXISTS 子查询来过滤出借阅了书籍 ID 为 1005 的记录,并选择相关联的读者姓名。请注意,这里使用了 WHERE 子句和 EXISTS 子查询来实现连接,将读者表和借阅表进行关联。
相关问题
以下用LEFT OUTER JOIN 和嵌套(NOT IN)两种方法来完成查询 (1)从来没有被借阅过的图书清单,结果显示图书名,作者。 (2)从来没有借过书的读者清单,结果显示读者姓名,读者单位。 (3)借过书号“11”这本书的读者姓名。(连接,嵌套,EXISTS三种方法完成此题。效率考虑,推荐实际使用时使用 INNER JOIN) (4)没借过书号“11”这本书的读者姓名。(NOT IN 嵌套,NOT EXISTS 两种种方法完成此题。效率考虑,推荐实际使用时使用 NOT EXISTS),相关字段可以用中文表示
1. 使用 LEFT OUTER JOIN 方法查询从来没有被借阅过的图书清单:
```
SELECT books.book_name, books.author
FROM books
LEFT OUTER JOIN borrow ON books.book_id = borrow.book_id
WHERE borrow.borrow_id IS NULL;
```
2. 使用 LEFT OUTER JOIN 方法查询从来没有借过书的读者清单:
```
SELECT readers.reader_name, readers.reader_unit
FROM readers
LEFT OUTER JOIN borrow ON readers.reader_id = borrow.reader_id
WHERE borrow.borrow_id IS NULL;
```
3. 使用 INNER JOIN 方法查询借过书号“11”这本书的读者姓名:
```
SELECT readers.reader_name
FROM readers
INNER JOIN borrow ON readers.reader_id = borrow.reader_id
WHERE borrow.book_id = '11';
```
使用嵌套查询的方法可以写成:
```
SELECT reader_name
FROM readers
WHERE reader_id IN (
SELECT reader_id
FROM borrow
WHERE book_id = '11'
);
```
使用 EXISTS 子查询的方法可以写成:
```
SELECT reader_name
FROM readers
WHERE EXISTS (
SELECT *
FROM borrow
WHERE borrow.reader_id = readers.reader_id
AND borrow.book_id = '11'
);
```
4. 使用 NOT IN 嵌套查询的方法查询没借过书号“11”这本书的读者姓名:
```
SELECT reader_name
FROM readers
WHERE reader_id NOT IN (
SELECT reader_id
FROM borrow
WHERE book_id = '11'
);
```
使用 NOT EXISTS 子查询的方法可以写成:
```
SELECT reader_name
FROM readers
WHERE NOT EXISTS (
SELECT *
FROM borrow
WHERE borrow.reader_id = readers.reader_id
AND borrow.book_id = '11'
);
```
注意,如果 borrow 表中存在 NULL 值,上述查询可能会出现意外结果。为了避免这种情况,可以使用 LEFT OUTER JOIN 和 IS NULL 或者 NOT EXISTS 子查询来进行查询。
创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。该软件是多文件程序,按文件给出代码,在主测试文件中设置菜单,由用户手动录入图书信息,用C++设计实现,设计Book类,Patron类,Date类
以下是多文件程序的代码实现:
首先是Date类的头文件 Date.h:
```c++
#ifndef DATE_H
#define DATE_H
class Date {
private:
int year, month, day;
public:
Date(int y, int m, int d);
int getYear() const;
int getMonth() const;
int getDay() const;
};
#endif
```
Date类的实现文件 Date.cpp:
```c++
#include "Date.h"
Date::Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
int Date::getYear() const {
return year;
}
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
```
接下来是Book类的头文件 Book.h:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
private:
std::string title;
std::string author;
std::string isbn;
bool is_available;
public:
Book(std::string t, std::string a, std::string i);
std::string getTitle() const;
std::string getAuthor() const;
std::string getIsbn() const;
bool getAvailability() const;
void setAvailability(bool b);
};
#endif
```
Book类的实现文件 Book.cpp:
```c++
#include "Book.h"
Book::Book(std::string t, std::string a, std::string i) {
title = t;
author = a;
isbn = i;
is_available = true;
}
std::string Book::getTitle() const {
return title;
}
std::string Book::getAuthor() const {
return author;
}
std::string Book::getIsbn() const {
return isbn;
}
bool Book::getAvailability() const {
return is_available;
}
void Book::setAvailability(bool b) {
is_available = b;
}
```
接下来是Patron类的头文件 Patron.h:
```c++
#ifndef PATRON_H
#define PATRON_H
#include <string>
class Patron {
private:
std::string name;
double fees;
public:
Patron(std::string n);
std::string getName() const;
double getFees() const;
void addFee(double f);
};
#endif
```
Patron类的实现文件 Patron.cpp:
```c++
#include "Patron.h"
Patron::Patron(std::string n) {
name = n;
fees = 0.0;
}
std::string Patron::getName() const {
return name;
}
double Patron::getFees() const {
return fees;
}
void Patron::addFee(double f) {
fees += f;
}
```
最后是Library类的头文件 Library.h:
```c++
#ifndef LIBRARY_H
#define LIBRARY_H
#include <vector>
#include "Book.h"
#include "Patron.h"
#include "Date.h"
struct Transaction {
Book book;
Patron patron;
Date date;
};
class Library {
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
public:
void addBook(Book b);
void addPatron(Patron p);
void lendBook(Book b, Patron p, Date d);
std::vector<std::string> getDebtors() const;
};
#endif
```
Library类的实现文件 Library.cpp:
```c++
#include "Library.h"
void Library::addBook(Book b) {
books.push_back(b);
}
void Library::addPatron(Patron p) {
patrons.push_back(p);
}
void Library::lendBook(Book b, Patron p, Date d) {
bool book_found = false;
bool patron_found = false;
int book_index = 0;
int patron_index = 0;
// Check if book exists
for (int i = 0; i < books.size(); i++) {
if (books[i].getIsbn() == b.getIsbn()) {
book_found = true;
book_index = i;
break;
}
}
// Check if patron exists
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i].getName() == p.getName()) {
patron_found = true;
patron_index = i;
break;
}
}
// If book or patron not found, report error
if (!book_found || !patron_found) {
std::cout << "Error: book or patron not found" << std::endl;
return;
}
// Check if book is available
if (!books[book_index].getAvailability()) {
std::cout << "Error: book is not available" << std::endl;
return;
}
// Check if patron owes fees
if (patrons[patron_index].getFees() > 0.0) {
std::cout << "Error: patron owes fees" << std::endl;
return;
}
// Lend book and record transaction
books[book_index].setAvailability(false);
Transaction t = { b, p, d };
transactions.push_back(t);
}
std::vector<std::string> Library::getDebtors() const {
std::vector<std::string> debtors;
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i].getFees() > 0.0) {
debtors.push_back(patrons[i].getName());
}
}
return debtors;
}
```
最后是主测试文件 main.cpp,包含一个简单的菜单:
```c++
#include <iostream>
#include "Library.h"
int main() {
Library library;
while (true) {
std::cout << "1. Add book" << std::endl;
std::cout << "2. Add patron" << std::endl;
std::cout << "3. Lend book" << std::endl;
std::cout << "4. List debtors" << std::endl;
std::cout << "5. Exit" << std::endl;
std::cout << "Enter choice: ";
int choice;
std::cin >> choice;
if (choice == 1) {
std::cout << "Enter title: ";
std::string title;
std::cin >> title;
std::cout << "Enter author: ";
std::string author;
std::cin >> author;
std::cout << "Enter ISBN: ";
std::string isbn;
std::cin >> isbn;
Book book(title, author, isbn);
library.addBook(book);
std::cout << "Book added" << std::endl;
}
else if (choice == 2) {
std::cout << "Enter name: ";
std::string name;
std::cin >> name;
Patron patron(name);
library.addPatron(patron);
std::cout << "Patron added" << std::endl;
}
else if (choice == 3) {
std::cout << "Enter ISBN: ";
std::string isbn;
std::cin >> isbn;
Book book("", "", isbn);
std::cout << "Enter patron name: ";
std::string name;
std::cin >> name;
Patron patron(name);
std::cout << "Enter date (year month day): ";
int year, month, day;
std::cin >> year >> month >> day;
Date date(year, month, day);
library.lendBook(book, patron, date);
}
else if (choice == 4) {
std::vector<std::string> debtors = library.getDebtors();
if (debtors.size() == 0) {
std::cout << "No debtors" << std::endl;
}
else {
std::cout << "Debtors:" << std::endl;
for (int i = 0; i < debtors.size(); i++) {
std::cout << debtors[i] << std::endl;
}
}
}
else if (choice == 5) {
break;
}
}
return 0;
}
```
阅读全文