用连接的方式改写代码SELECT readerName FROM tb_reader WHERE NOT EXISTS (SELECT * FROM tb_booklend WHERE bookCode = '11' AND tb_reader.readerId = readerId);
时间: 2024-03-23 20:42:36 浏览: 52
以下是使用连接的方式改写的代码:
```
SELECT readerName
FROM tb_reader
LEFT JOIN tb_booklend ON tb_reader.readerId = tb_booklend.readerId AND tb_booklend.bookCode = '11'
WHERE tb_booklend.readerId IS NULL;
```
这样就用连接的方式将原始代码改写了。
相关问题
SELECT readerName FROM tb_reader WHERE EXISTS (SELECT * FROM tb_booklend WHERE bookCode = '11' AND tb_reader.readerId = readerId); 用连接的方式改写代码
以下是使用连接方式改写的代码:
```
SELECT readerName
FROM tb_reader
JOIN tb_booklend ON tb_reader.readerId = tb_booklend.readerId
WHERE bookCode = '11';
```
这样就用连接的方式将原始代码改写了。
在下面这两个表中查询郭玉姣”借阅的图书名称CREATE TABLE `record` ( `recordid` INT(11) NOT NULL COMMENT '记录号', `readerid` CHAR(13) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '读者号', `bookid` CHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '图书号', `outdate` DATE NOT NULL COMMENT '借出日期', `indate` DATE DEFAULT NULL COMMENT '还入日期', `state` VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '当前状态', PRIMARY KEY (`recordid`), KEY `readerid` (`readerid`), KEY `bookid` (`bookid`), CONSTRAINT `record_ibfk_1` FOREIGN KEY (`readerid`) REFERENCES `reader` (`readerid`), CONSTRAINT `record_ibfk_2` FOREIGN KEY (`bookid`) REFERENCES `book` (`bookid`) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;CREATE TABLE `reader` ( `readerid` CHAR(13) NOT NULL COMMENT '读者编号', `readername` VARCHAR(20) NOT NULL COMMENT '姓名', `typeid` TINYINT(4) DEFAULT NULL COMMENT '类别编号', `birthday` DATE DEFAULT NULL COMMENT '生日', `sex` CHAR(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '男' COMMENT '性别', `tel` CHAR(11) DEFAULT NULL COMMENT '电话', `enrolldate` DATE NOT NULL COMMENT '注册日期', `state` CHAR(10) DEFAULT NULL COMMENT '状态', PRIMARY KEY (`readerid`), KEY `typeid` (`typeid`), CONSTRAINT `reader_ibfk_1` FOREIGN KEY (`typeid`) REFERENCES `readertype` (`typeid`), CONSTRAINT `reader_chk_1` CHECK (((`sex` = _utf8mb4'男') OR (`sex` = _utf8mb4'女'))), CONSTRAINT `reader_chk_2` CHECK (((`state` = _utf8mb3'有效') OR (`state` = _utf8mb3'无效'))) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
可以使用以下 SQL 语句查询:
SELECT book.bookname
FROM record
JOIN reader ON record.readerid = reader.readerid
JOIN book ON record.bookid = book.bookid
WHERE reader.readername = '郭玉姣' AND record.state = '在借';
这条 SQL 语句使用了三个表的连接,以查询借阅记录表中读者姓名为“郭玉姣”且当前状态为“在借”的记录,然后通过连接读者表和图书表,查询出对应的图书名称。
阅读全文