成都10班邓旭琳 发表于 2021-11-23 18:53:33

多表连接

inner join :内连接
select * from aa inner join cc on aa.id=cc.s_id :显示aa的id 与cc的s_id相等的 数据    (显示aa.id = cc.s_id的数据)
leftjoin :左连接
select * from aa left join cc on aa.id=cc.s_id :以左边的aa表为主,cc表缺少的数据为空填充,多余的数据直接去掉
rightjoin :右连接
select * from aa right join cc on aa.id=cc.s_id :以右边的cc表为主
union :硬连接(机械连接)
select * from aa union select * from cc : 直接将cc表中的数据连在aa表后面(前提是两个表 字段个数相同)
基本连接 :select * from aa,cc where aa.id=cc.s_id查询两个表中aa.id=cc.s_id的数据
                  select name,score from aa,cc where aa.id=cc.s_id and name='zhangsan': 查询张三的成绩
嵌套=:select id from aa where name='zhangsan';查出张三的id
            select score from cc where s_id=(select id from aa where name='zhangsan');   用id查出张三的成绩
嵌套in   :select id from aa where name='zhangsan';查出张三的id
            select score from cc where s_id in(select id from aa where name='zhangsan');   用id查出张三的成绩
临时表 : select * from aa,cc where aa.id=cc.s_id;查询两个表中aa.id=cc.s_id的数据, 作为临时表
             select name,score from(select * from aa,cc where aa.id=cc.s_id)twhere name='zhangsan';
             从临时表中查出张三的成绩(在用临时表时,需要在括号后面取别名)


页: [1]
查看完整版本: 多表连接