2021-06-01

第7章:连接查询

三表连接查询的语法如下:

select  字段列表  from  表1  join 表2  on  表1.字段名1=表2.字段名2  join  表3  on  表2.字段名2=表3.字段名3  [where<条件表达式>] 

C:\Users\123\Documents\Tencent Files\2118400963\FileRecv

1.获取所有非空调车的车牌号、型号和司机姓名、所属线路的线路号、起点站和终点站信息

select name 司机姓名,plateNo 车牌号,model 型号,lineNo 线路号,from_station 起点站,
end_station 终点站
from vehicle v inner join driver d on 
v.driverID =d.driverID
inner join line l on
v.lineID=l.lineID 
where type='非空调车'

2.获取公交二公司所有司机信息。要求输出司机姓名、身份证、性别和电话;

select name 司机姓名,liceseNo 身份证,gender 性别,phone 电话 
from vehicle v inner join driver d on
v.driverID=d.driverID 
join line l on
v.lineID=l.lineID
where company='公交二公司';

  3.查询所有非空调车的车牌号、型号、线路号、起点站、终点站;

select plateNo 车牌号,model 型号,lineNo 线路号,from_station 起点站,end_station 终点站
from vehicle v left join line l on 
v.lineID=l.lineID 
where type='非空调车';

4.显示所有司机基本信息,并查询其所驾驶车辆和行驶线路的相关信息,要求输出司机姓名、性别、电话、车牌号、型号、线路号、起点站、终点站;

select name 司机姓名,gender 性别,phone 电话,plateNo 车牌号,model 型号,lineNo 线路号,
from_station 起点站,end_station 终点站 from
driver d left join vehicle v on
d.driverID=v.driverID 
left join line l on
v.lineID=l.lineID;

5.获取订单ID为4的订购明细信息,要求输出商品名、单价和件数;

select goodsName 商品名,unitPrice 单价,quantity 件数 from
orders o,ordersdetail od,goods g 
where o.ordersID=od.ordersID and
od.goodsID=g.goodsID and
o.ordersID=4;

6.获取客户“王传华”所下订单详细信息,要求显示客户姓名,订单id、下单日期、商品名、单价和件数。

select o.ordersID 订单ID,ordersDate 下单日期,goodsName 商品名,unitPrice 单价,
quantity 件数 from
customer c,orders o,ordersdetail od,goods g
where c.customerID=o.customerID and
o.ordersID=od.ordersID and
od.goodsID=g.goodsID and
cName='王传华';

7.使用左外连接获取所有客户的基本信息以及订购信息,要求输出客户姓名、电话、订单ID和订单时间;

select cName 姓名,ordersID 订单ID,ordersDate 下单日期 from

customer c left join orders o on

c.customerID=o.customerID;

8.使用左外连接获取所有客户的基本信息以及订购信息,要求输出客户姓名、电话、订单ID和订单时间;

​
select cName 姓名,ordersID 订单ID,ordersDate 下单日期 from

customer c right join orders o on

c.customerID=o.customerID;

​

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二十的十七

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

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

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

打赏作者

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

抵扣说明:

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

余额充值