关于SQL语句

[复制链接]
查看11 | 回复5 | 2008-8-12 22:07:47 | 显示全部楼层 |阅读模式
假设有两张表,bookInfo和bookBorrow
分别表示图书信息与图书的借阅情况
bookInfo 的字段有 bookid bookname 表示图书的id,和图书名称
bookBorrow 的字段有 bookid returned 表示图书的id,和图书的返还情况
例如
在bookInfo 中
bookidbookname
1
a
2
b
3
c
4
d
5
e
在bookborrow中
bookidreturned
1

2

3
已预约
问如何才能根据returned 的情况,得到一张这样的表
bookid booknamereturn
1
a

2
b

3
c
已预约
4
d

5
e

是这样的,如果没有出现在借阅的表(bookborrow)中,就默认为在图书馆中,所以默认的returned值为'是'
谢谢大家的回答..

回复

使用道具 举报

千问 | 2008-8-12 22:07:47 | 显示全部楼层
select bookInfo.bookid bookname returned from bookInfo left join bookBorrow on bookInfo.bookid=bookBorrow.bookid 可以得到这样的表bookid bookname return 1 a 是 2 b 否 3 c 已预约 4 d null 5 e null 你的题目逻辑上说不通,不过我猜你要的结果是这样的
回复

使用道具 举报

千问 | 2008-8-12 22:07:47 | 显示全部楼层
select a.bookid a.bookname (case when b.return is null then "是" else b.return end) as returnfrom bookInfo aleft joinbookborrow bon a.bookid=b.bookid
回复

使用道具 举报

千问 | 2008-8-12 22:07:47 | 显示全部楼层
select bookInfo.bookid ,bookInfo.bookname,bookBorrow.returned into 要生成表 from bookInfo , bookBorrow where bookInfo.bookid=bookBorrow.bookid
回复

使用道具 举报

千问 | 2008-8-12 22:07:47 | 显示全部楼层
一楼的没错,二的左连接更严谨!
回复

使用道具 举报

千问 | 2008-8-12 22:07:47 | 显示全部楼层
select bookInfo.bookid bookname returned from bookInfo and bookBorrow where bookInfo.bookid=bookBorrow.bookid 结果是1 a 是 2 b 否 3 c 已预约 后面 4 d 是 5 e 是 哪里来的...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行