成都10班-戴绍武 发表于 2021-11-23 18:24:47

数据库表与表之间的操作

1、内连接 inner join对于两个表中有字段的值相同,就可以通过内连接进行拼接
select * from aa inner join cc on aa.id=cc.s_id
2、左连接 left join以左边的表为主,对于不符合条件的数据显示null
select * from aa left join cc on aa.id=cc.s_id
3、右连接 right join以右边的表为主,对于不符合条件的数据显示null
select * from aa right join cc on aa.id=cc.s_id
4、基本连接(对于两个表当中有字段的值相同就可以通过基本连接进行拼接)
select * from aa,cc where aa.id=cc.s_id
5、硬链接 (机械的拼接)条件是aa表的字段数目需要和cc表的字段数目一样
select * from aa union select * from cc
6、临时表(通过sql语句查询出来显示的表都可以作为临时表)
select name,score from (select * from aa,cc where aa.id=cc.s_id)
7、嵌套=方法
select score from cc where s_id=(select id from aa where name='zhangsan')
8、嵌套in方法
select score from cc where s_id in(select id from aa wherename='zhangsan')
9、基本连接方法
select name,score from aa,cc where aa.id=cc.s_id and name='zhangsan'
求出谁没有参加考试?
一、嵌套方法
select name from aa where id not in(select id from aa,cc where aa.id=cc.s_id)
二、左连接+临时表
select name from (select * from aa left join cc on aa.id=cc.s_id)t where score is null
三、嵌套
select name from aa where id not in(select s_id from cc where score is not null)

页: [1]
查看完整版本: 数据库表与表之间的操作