mysql学习:复杂查询

首先用以下语句创建表或加入数据:

create table students(id int(4) ,name varchar(20),birth date,primary key(id));
insert into students values(5,'e','1999-07-26');

create table borrows(wid int(4) ,bid int(4));
insert into borrows values(1,1);

create table books(id int(4) auto_increment,name varchar(20),primary key(id));
insert into books values(1,'aaa');
insert into books values(0,'bbb');

学生表,书籍表,借阅表分别如下:

mysql> select * from students;
+----+------+------------+
| id | name | birth      |
+----+------+------------+
|  1 | a    | 2021-09-22 |
|  2 | b    | 2000-01-02 |
|  3 | c    | 2002-04-24 |
|  4 | d    | 2021-10-27 |
|  5 | e    | 1999-07-26 |
|  6 | h    | 1999-07-26 |
|  7 | g    | 2009-12-08 |
|  8 | m    | 2000-07-21 |
+----+------+------------+
8 rows in set (0.00 sec)

mysql> select * from books;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbb  |
|  3 | ccc  |
|  4 | ddd  |
|  5 | eee  |
|  6 | fff  |
+----+------+
6 rows in set (0.00 sec)

mysql> select * from borrows;
+------+------+
| wid  | bid  |
+------+------+
|    1 |    1 |
|    1 |    5 |
|    2 |    4 |
|    3 |    1 |
|    4 |    3 |
|    5 |    2 |
|    6 |    1 |
+------+------+
7 rows in set (0.00 sec)

查询借阅书籍aaa的学生:

mysql> select s.id sid,s.name sname from students s,borrows w,books b
    -> where s.id=w.wid and w.bid=b.id and b.name='aaa' group by sid;

+----+------+------------+
| id | name | birth      |
+----+------+------------+
|  1 | a    | 2021-09-22 |
|  3 | c    | 2002-04-24 |
|  6 | h    | 1999-07-26 |
+----+------+------------+
3 rows in set (0.00 sec)
mysql> select * from students where id in(select distinct wid from borrows 
where bid in(select id from books where name = 'aaa'));

+-----+-------+
| sid | sname |
+-----+-------+
|   1 | a     |
|   3 | c     |
|   6 | h     |
+-----+-------+
3 rows in set (0.00 sec)

查询没有人借阅的书:

mysql> select * from books where name  in(select distinct name from books
    -> where id not in(select bid from borrows));
    
+----+------+
| id | name |
+----+------+
|  6 | fff  |
+----+------+
1 row in set (0.00 sec)
mysql> select * from books where name not in(select b.name from borrows w, books b where w.bid=b.id);

+----+------+
| id | name |
+----+------+
|  6 | fff  |
+----+------+
1 row in set (0.00 sec)

查询和a有共同读书爱好的人:

mysql> select * from students where id in
    -> (select wid from borrows where bid in(select bid from borrows where wid=1));
    
+----+------+------------+
| id | name | birth      |
+----+------+------------+
|  1 | a    | 2021-09-22 |
|  3 | c    | 2002-04-24 |
|  6 | h    | 1999-07-26 |
+----+------+------------+
3 rows in set (0.00 sec)

查询最流行的人名:(没有最流行的=_=)

mysql> select name,count(*) cnt from students group by name
    -> order by cnt desc;
    
+------+-----+
| name | cnt |
+------+-----+
| b    |   1 |
| c    |   1 |
| d    |   1 |
| e    |   1 |
| h    |   1 |
| g    |   1 |
| m    |   1 |
| a    |   1 |
+------+-----+
8 rows in set (0.00 sec)

查询最流行的书籍:

mysql> select * from books where id in(
    -> select bid from borrows group by bid order by count(*)desc limit 1);
//达咩!老老实实用=吧
mysql> select * from books where id =(
    -> select bid from borrows group by bid order by count(*)desc limit 1);
    
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
+----+------+
1 row in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊这怎么会这样

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值