栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库系统原理与应用教程(066)—— MySQL 练习题:操作题 71-81(十):连接查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库系统原理与应用教程(066)—— MySQL 练习题:操作题 71-81(十):连接查询

数据库系统原理与应用教程(066)—— MySQL 练习题:操作题 71-81(十):连接查询

71、连接查询(1)

数据表:Customers,包含顾客名称:cust_name、顾客 id:cust_id,表中数据如下:

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex

数据表:Orders(订单信息表),包含订单号:order_num,顾客 id:cust_id,表中数据如下:

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

【问题】编写 SQL 语句,查询 Customers 表中的顾客名称(cust_name)和 Orders 表中的相关订单号(order_num),并按顾客名称再按订单号对结果进行升序排序。查询结果如下:

cust_nameorder_num
ana5
andya1
bena2
hexa7
toma4
tonya3

表结构及数据如下:

 

解答:

mysql> select c.cust_name, o.order_num
    -> from Customers c join Orders o on c.cust_id = o.cust_id
    -> order by c.cust_name, o.order_num;
+-----------+-----------+
| cust_name | order_num |
+-----------+-----------+
| an        | a5        |
| andy      | a1        |
| ben       | a2        |
| hex       | a7        |
| tom       | a4        |
| tony      | a3        |
+-----------+-----------+
6 rows in set (0.00 sec)

72、连接查询(2)

数据表:Customers,包含顾客名称:cust_name,顾客id:cust_id,表中数据如下:

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex

数据表:Orders(订单信息表),包含订单号:order_num,顾客id:cust_id,表中数据如下:

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

数据表:OrderItems,包含商品订单号:order_num,订购数量:quantity,单价:item_price,表中数据如下:

order_numquantityitem_price
a1100010
a220010
a31015
a42550
a51525
a777

【问题】查询顾客名称、订单号以及每个订单的总价(OrderTotal),并按顾客名称再按订单号对结果进行升序排序。查询结果如下:

cust_nameorder_numOrderTotal
ana5375
andya110000
bena22000
hexa749
toma41250
tonya3150

表结构及数据如下:

 

解答:

mysql> select cust_name, o.order_num, 
    ->        sum(oi.quantity * oi.item_price) OrderTotal
    -> from Customers c join Orders o on c.cust_id = o.cust_id
    ->      join OrderItems oi on o.order_num = oi.order_num
    -> group by cust_name, o.order_num
    -> order by cust_name, o.order_num;
+-----------+-----------+------------+
| cust_name | order_num | OrderTotal |
+-----------+-----------+------------+
| an        | a5        |        375 |
| andy      | a1        |      10000 |
| ben       | a2        |       2000 |
| hex       | a7        |         49 |
| tom       | a4        |       1250 |
| tony      | a3        |        150 |
+-----------+-----------+------------+
6 rows in set (0.00 sec)

73、连接查询(3)

数据表:OrderItems,表中数据如下:

prod_idorder_num
BR01a0001
BR01a0002
BR02a0003
BR02a0013

数据表:Orders,表中数据如下:

order_numcust_idorder_date
a0001cust102022-01-01 00:00:00
a0002cust12022-01-01 00:01:00
a0003cust12022-01-02 00:00:00
a0013cust22022-01-01 00:20:00

【问题】编写 SQL 语句,查询哪些订单购买了 prod_id 为 “BR01” 的产品,显示每个产品对应的顾客 ID(cust_id)和订单日期(order_date),按订购日期对结果进行升序排序。查询结果如下:

cust_idorder_date
cust102022-01-01 00:00:00
cust12022-01-01 00:01:00

表结构及数据如下:

 

解答:

-- 使用子查询
mysql> select cust_id, order_date
    -> from Orders where order_num in 
    ->     (select order_num from OrderItems where prod_id = 'BR01')
    -> order by order_date;
+---------+---------------------+
| cust_id | order_date          |
+---------+---------------------+
| cust10  | 2022-01-01 00:00:00 |
| cust1   | 2022-01-01 00:01:00 |
+---------+---------------------+
2 rows in set (0.00 sec)

-- 使用连接查询
mysql> select o.cust_id, o.order_date
    -> from Orders o join OrderItems oi on o.order_num = oi.order_num
    -> where oi.prod_id = 'BR01'
    -> order by o.order_date;
+---------+---------------------+
| cust_id | order_date          |
+---------+---------------------+
| cust10  | 2022-01-01 00:00:00 |
| cust1   | 2022-01-01 00:01:00 |
+---------+---------------------+
2 rows in set (0.00 sec)

74、连接查询(4)

数据表:OrderItems,表中数据如下:

prod_idorder_num
BR01a0001
BR01a0002
BR02a0003
BR02a0013

数据表:Orders,表中数据如下:

order_numcust_idorder_date
a0001cust102022-01-01 00:00:00
a0002cust12022-01-01 00:01:00
a0003cust12022-01-02 00:00:00
a0013cust22022-01-01 00:20:00

数据表:Customers,表中数据如下:

cust_idcust_email
cust10cust10@cust.com
cust1cust1@cust.com
cust2cust2@cust.com

【问题】查询购买 prod_id 为 “BR01” 的产品的所有顾客的电子邮件。查询结果如下:

cust_email
cust10@cust.com
cust1@cust.com

表结构及数据如下:

 

解答:

-- 使用子查询
mysql> select cust_email from Customers where cust_id in 
    ->     (select cust_id from Orders where order_num in 
    ->          (select order_num from OrderItems where prod_id = 'BR01')
    ->     );
+-----------------+
| cust_email      |
+-----------------+
| cust10@cust.com |
| cust1@cust.com  |
+-----------------+
2 rows in set (0.00 sec)

-- 使用连接查询
mysql> select c.cust_email 
    -> from Customers c join Orders o on c.cust_id = o.cust_id
    ->     join OrderItems oi on o.order_num = oi.order_num
    -> where prod_id = 'BR01';
+-----------------+
| cust_email      |
+-----------------+
| cust10@cust.com |
| cust1@cust.com  |
+-----------------+
2 rows in set (0.00 sec)

75、连接查询(5)

数据表:OrderItems,表中数据如下:

order_numitem_pricequantity
a110105
a211100
a21200
a421121
a5510
a2119
a775

数据表:Orders,表中数据如下:

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

数据表:Customers,表中数据如下:

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex

【问题】编写 SQL 语句,查询订单总价不小于 1000 的客户名称和总额(item_price * quantity),按总额对结果进行排序。查询结果如下:

cust_nametotal_price
andy1050
ben1319
tom2242

表结构及数据如下:

 

解答:

mysql> select c.cust_name,
    ->        sum(oi.item_price * oi.quantity) total_price
    -> from OrderItems oi join Orders o on oi.order_num = o.order_num
    ->     join Customers c on o.cust_id = c.cust_id
    -> group by c.cust_name
    -> having total_price >= 1000
    -> order by total_price;
+-----------+-------------+
| cust_name | total_price |
+-----------+-------------+
| andy      |        1050 |
| ben       |        1319 |
| tom       |        2242 |
+-----------+-------------+
3 rows in set (0.00 sec)

76、连接查询(6)

数据表:Customers,表中数据如下:

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex

数据表:Orders,表中数据如下:

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

【问题】编写 SQL语句,查询每个顾客的名称和所有的订单号,根据顾客姓名 cust_name 升序排序。查询结果如下:

cust_nameorder_num
ana5
andya1
bena2
hexa7
toma4
tonya3

表结构及数据如下:

 

解答:

mysql> select c.cust_name, o.order_num
    -> from Customers c join Orders o on c.cust_id = o.cust_id
    -> order by c.cust_name;
+-----------+-----------+
| cust_name | order_num |
+-----------+-----------+
| an        | a5        |
| andy      | a1        |
| ben       | a2        |
| hex       | a7        |
| tom       | a4        |
| tony      | a3        |
+-----------+-----------+
6 rows in set (0.00 sec)

77、连接查询(7)

数据表:Orders,表中数据如下:

order_numcust_id
a1cust10
a2cust1
a3cust2
a4cust22
a5cust221
a7cust2217

数据表:Customers,表中数据如下:

cust_idcust_name
cust10andy
cust1ben
cust2tony
cust22tom
cust221an
cust2217hex
cust40ace

【问题】检索每个顾客的名称和所有的订单号,列出所有的顾客,即使他们没有下过订单。查询结果如下:

cust_nameorder_num
aceNULL
ana5
andya1
bena2
hexa7
toma4
tonya3

表结构及数据如下:

 

解答:

mysql> select c.cust_name, o.order_num
    -> from Customers c left join Orders o on c.cust_id = o.cust_id
    -> order by c.cust_name;
+-----------+-----------+
| cust_name | order_num |
+-----------+-----------+
| ace       | NULL      |
| an        | a5        |
| andy      | a1        |
| ben       | a2        |
| hex       | a7        |
| tom       | a4        |
| tony      | a3        |
+-----------+-----------+
7 rows in set (0.00 sec)

78、连接查询(8)

数据表:Products,表中数据为:

prod_idprod_name
a0001egg
a0002sockets
a0013coffee
a0003cola
a0023soda

数据表:OrderItems,表中数据为:

prod_idorder_num
a0001a105
a0002a1100
a0002a200
a0013a1121
a0003a10
a0003a19
a0003a5

【问题】编写 SQL 语句,查询产品名称(prod_name)和与之相关的订单号(order_num)的列表,并按照产品名称升序排序。查询结果如下:

prod_nameorder_num
coffeea1121
colaa5
colaa19
colaa10
egga105
socketsa200
socketsa1100
sodaNULL

表结构及数据如下:

 

解答:

mysql> select p.prod_name, oi.order_num
    -> from Products p left join OrderItems oi on p.prod_id =  oi.prod_id
    -> order by p.prod_name;
+-----------+-----------+
| prod_name | order_num |
+-----------+-----------+
| coffee    | a1121     |
| cola      | a10       |
| cola      | a19       |
| cola      | a5        |
| egg       | a105      |
| sockets   | a1100     |
| sockets   | a200      |
| soda      | NULL      |
+-----------+-----------+
8 rows in set (0.00 sec)

79、连接查询(9)

数据表:Products,表中数据如下:

prod_idprod_name
a0001egg
a0002sockets
a0013coffee
a0003cola
a0023soda

数据表:OrderItems,表中数据如下:

prod_idorder_num
a0001a105
a0002a1100
a0002a200
a0013a1121
a0003a10
a0003a19
a0003a5

【问题】查询产品名称(prod_name)和每一项产品的总订单数,并按产品名称升序排序。查询结果如下:

prod_nameorders
coffee1
cola3
egg1
sockets2
soda0

解答:

mysql> select p.prod_name, count(order_num) orders
    -> from Products p left join OrderItems oi on p.prod_id =  oi.prod_id
    -> group by p.prod_name
    -> order by p.prod_name;
+-----------+--------+
| prod_name | orders |
+-----------+--------+
| coffee    |      1 |
| cola      |      3 |
| egg       |      1 |
| sockets   |      2 |
| soda      |      0 |
+-----------+--------+
5 rows in set (0.00 sec)

80、连接查询(10)

数据表:Vendors,表中数据如下:

vend_id
a0002
a0013
a0003
a0010

数据表:Products,表中数据如下:

vend_idprod_id
a0001egg
a0002prod_id_iphone
a00113prod_id_tea
a0003prod_id_vivo phone
a0010prod_id_huawei phone

【问题】列出供应商(Vendors 表中的 vend_id)及可供产品的数量(包括没有产品的供应商),根据 vend_id 升序排序。查询结果如下:

vend_idprod_id
a00021
a00031
a00101
a00130

表结构及数据如下:

 

解答:

mysql> select v.vend_id, count(prod_id) prod_id
    -> from Vendors v left join Products p on v.vend_id = p.vend_id
    -> group by v.vend_id
    -> order by v.vend_id;
+---------+---------+
| vend_id | prod_id |
+---------+---------+
| a0002   |       1 |
| a0003   |       1 |
| a0010   |       1 |
| a0013   |       0 |
+---------+---------+
4 rows in set (0.00 sec)

81、union 联合查询

数据表:OrderItems,表中数据如下:

prod_idquantity
a0001105
a0002100
a0002200
a00131121
a000310
a000319
a00035
BNBG10002

【问题】查询数量(quantity)为 100 的产品信息和产品 id(prod_id)以【BNBG】开头的产品信息,按产品 id 对结果进行升序排序。查询结果如下:

prod_idquantity
a0002100
BNBG10002

表结构及数据如下:

 

解答:

-- 使用 union
mysql> select * from OrderItems where quantity = 100
    -> union
    -> select * from OrderItems where prod_id like 'BNBG%'
    -> order by prod_id; 
+---------+----------+
| prod_id | quantity |
+---------+----------+
| a0002   | 100      |
| BNBG    | 10002    |
+---------+----------+
2 rows in set (0.03 sec)

-- 使用 or 构造查询条件
mysql> select * from OrderItems 
    -> where quantity = 100 or prod_id like 'BNBG%'
    -> order by prod_id; 
+---------+----------+
| prod_id | quantity |
+---------+----------+
| a0002   | 100      |
| BNBG    | 10002    |
+---------+----------+
2 rows in set (0.00 sec)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号