SQL Server从3个表中选择

用户名

我的数据库BooksBrowserMovement中有三个表

图书

BookID      Title                          Author                    Category        Published
----------- ------------------------------ ------------------------- --------------- ----------
101         Ulysses                        James Joyce               Fiction         1922-06-16
102         Huckleberry Finn               Mark Twain                Fiction         1884-03-24
103         The Great Gatsby               F. Scott Fitzgerald       Fiction         1925-06-17
104         1984                           George Orwell             Fiction         1949-04-19
105         War and Peace                  Leo Tolstoy               Fiction         1869-08-01
106         Gullivers Travels              Jonathan Swift            Fiction         1726-07-01
107         Moby Dick                      Herman Melville           Fiction         1851-08-01
108         Pride and Prejudice            Jane Austen               Fiction         1813-08-13
110         The Second World War           Winston Churchill         NonFiction      1953-09-01
111         Relativity                     Albert Einstein           NonFiction      1917-01-09
112         The Right Stuff                Tom Wolfe                 NonFiction      1979-09-07
121         Hitchhikers Guide to Galaxy    Douglas Adams             Humour          1975-10-27
122         Dad Is Fat                     Jim Gaffigan              Humour          2013-03-01
131         Kick-Ass 2                     Mark Millar               Comic           2012-03-03
133         Beautiful Creatures: The Manga Kami Garcia               Comic           2014-07-01

借款人

BorrowerID  Name                      Birthday
----------- ------------------------- ----------
2           Bugs Bunny                1938-09-08
3           Homer Simpson             1992-09-09
5           Mickey Mouse              1928-02-08
7           Fred Flintstone           1960-06-09
11          Charlie Brown             1965-06-05
13          Popeye                    1933-03-03
17          Donald Duck               1937-07-27
19          Mr. Magoo                 1949-09-14
23          George Jetson             1948-04-08
29          SpongeBob SquarePants     1984-08-04
31          Stewie Griffin            1971-11-17

移动

MoveID      BookID      BorrowerID  DateOut    DateIn     ReturnCondition
----------- ----------- ----------- ---------- ---------- ---------------
1           131         31          2012-06-01 2013-05-24 good
2           101         23          2012-02-10 2012-03-24 good
3           102         29          2012-02-01 2012-04-01 good
4           105         7           2012-03-23 2012-05-11 good
5           103         7           2012-03-22 2012-04-22 good
6           108         7           2012-01-23 2012-02-12 good
7           112         19          2012-01-12 2012-02-10 good
8           122         11          2012-04-14 2013-05-01 poor
9           106         17          2013-01-24 2013-02-01 good
10          104         2           2013-02-24 2013-03-10 bitten
11          121         3           2013-03-01 2013-04-01 good
12          131         19          2013-04-11 2013-05-23 good
13          111         5           2013-05-22 2013-06-22 poor
14          131         2           2013-06-12 2013-07-23 bitten
15          122         23          2013-07-10 2013-08-12 good
16          107         29          2014-01-01 2014-02-14 good
17          110         7           2014-01-11 2014-02-01 good
18          105         2           2014-02-22 2014-03-02 bitten

我可以用来查询最老的借书者借的书是什么?

我是SQL的新手,正在使用Microsoft SQL Server 2014

萨什·辛哈(Sash Sinha)

这是两种不同的解决方案:

首先使用两个子查询和一个相等联接

select Title
from Books b , Movement m
where b.BookID = m.BookID and m.BorrowerID = (select BorrowerID
                                              from Borrowers
                                              where Birthday = (select MIN(Birthday)
                                                                from Borrowers))

使用两个等联接和一个子查询:

select Title
from Books b, Borrowers r, Movement m
where b.BookID = m.BookID
and m.BorrowerID = r.BorrowerID
and Birthday = (select MIN(Birthday) from Borrowers)

以上两个查询都给出以下答案:

Title
------------------------------
Relativity

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从两个表SQL Server中选择列

从SQL Server表中选择n个随机行

sql server从两个表中选择null或value

SQL使用GROUP BY从3个表中选择数据

如何使用GROUP BY从SQL的3个表中选择数据?

使用3个不同的表在SQL Update中选择Statement

从SQL中的3个表中选择信息

SQL:如何从这3个表中选择数据

SQL查询基于列值从3个选项中选择并从其他表中选择

SQL Server 2008:连接3个表并针对每个父记录从子表中选择最后输入的记录

从SQL Server表中选择备用行

SQL从2个不同的表中选择

SQL Server:插入到表中,从另一个表中选择

SQL Server:从表A中选择列,并从表B中加入一个COUNT

SQL Server:从前3个类别中选择前3个项

SQL Server - 如何从不同的行但在同一个表中选择值

如何从 SQL Server 的另一个表中选择“组”列名?

SQL Server:从一个表中选择与层次结构相关的项目

SQL Server每小时或每天从表中选择1个单个数据点

SQL Server:从其他2个表中选择相同结果

SQL Server 2005 从表 1 和表 2 中选择数据,但如果表 2 的列 1 值为空,则从表 3 中选择数据

从每组的3个表中选择具有最新日期的sql行

在SQL Server中选择一个列名作为另一个表的行数据

从 SQL Server 中的表中选择除了重复记录

SQL Server,从不同表的2列中选择

SQL Server从不同的表中选择数据

F#从SQL Server表同义词中选择

SQL Server:从表中选择最近日期的条目

如何从SQL Server中的表中选择别名?