数据库几个连接的测试

晚上看了一个同事在写sql,觉得自己有段时间没写sql了,突然好想写写,然后就在Oracled

的PL/SQL Developer 写了几个简单的语句来玩下,并且写写运行的结果。

试验准备:
[list]
[*]
[*]teacher,student,tea_stu 三个表 表的字段如下:
[*]teacer : teaid , name , age
[*]student: stuid , name , age
[*]tea_stu: teaid , stuid
[/list]

假设tea_stu 这个表现在有13条记录 teacher 有4条记录 student 有4条记录

select teaid from tea_stu union all select teaid from teacher

共17条记录 = 两个表记录的相加的总和 即没有去掉重复

select teaid from tea_stu union select teaid from teacher

共4条记录 = 两个表记录的相加的总和 并去掉重复

select * from tea_stu , teacher

共52条记录 就是笛卡尔积 相当于

select * from tea_stu , teacher on 1=1 (inner join 一定要加 on + 条件。)

select * from tea_stu left join teacher on tea_stu.teaid = teacher.teaid 共13条记录

因为tea_stu是主表,有13条记录 teacher表是从表,如果主表中有和从表相对应的记录 整

条记录都不会为空。如果没有相对应的记录的话,右边就是显示为空。

补充:inner join 和 left join 都一定要加 on a.id = b.id这样的条件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章