表1:teachersql
+----+--------+ | id | name | +----+--------+ | 1 | 刘德华 | | 2 | 张学友 | | 4 | 黎明 | +----+--------+
表2:studentcode
+----+------+--------+ | id | name | tea_id | +----+------+--------+ | 1 | 张三 | 1 | | 2 | 李四 | 1 | | 3 | 王五 | 1 | | 4 | 赵六 | 2 | | 5 | 孙七 | 2 | +----+------+--------+
1.内链接:在每一个表中找出符合条件的共有记录。[x inner join y on...]
第一种写法:(至关于多表查询,用的是where!)table
select t.*,s.* from teacher t,student s where t.id=s.tea_id;
第二种写法:(inner省略了)class
select t.*,s.* from teacher t join student s on t.id=s.tea_id;
第三种写法:软件
select t.*,s.* from teacher t inner join student s on t.id=s.tea_id;
+----+--------+----+------+--------+ | id | name | id | name | tea_id | +----+--------+----+------+--------+ | 1 | 刘德华 | 1 | 张三 | 1 | | 1 | 刘德华 | 2 | 李四 | 1 | | 1 | 刘德华 | 3 | 王五 | 1 | | 2 | 张学友 | 4 | 赵六 | 2 | | 2 | 张学友 | 5 | 孙七 | 2 | +----+--------+----+------+--------+
2.外链接有三种方式:左链接,右链接和全链接。
2.1 左链接:根据左表的记录,在被链接的右表中找出符合条件的记录与之匹配
找不到与左表匹配的,用null表示。[x left [outer] join y on...]
第一种写法:select
select t.*,s.* from teacher t left join student s on t.id=s.tea_id;
第二种写法:查询
select t.*,s.* from teacher t left outer join student s on t.id=s.tea_id;
+----+--------+------+------+--------+ | id | name | id | name | tea_id | +----+--------+------+------+--------+ | 1 | 刘德华 | 1 | 张三 | 1 | | 1 | 刘德华 | 2 | 李四 | 1 | | 1 | 刘德华 | 3 | 王五 | 1 | | 2 | 张学友 | 4 | 赵六 | 2 | | 2 | 张学友 | 5 | 孙七 | 2 | | 4 | 黎明 | NULL | NULL | NULL | +----+--------+------+------+--------+
2.2 右链接:根据右表的记录,在被链接的左表中找出符合条件的记录与之匹配,
找不到匹配的,用null填充。[x right [outer] join y on...]
第一种写法:tab
select t.*,s.* from teacher t right join student s on t.id=s.tea_id;
第二种写法:co
select t.*,s.* from teacher t right outer join student s on t.id=s.tea_id;
+------+--------+----+------+--------+ | id | name | id | name | tea_id | +------+--------+----+------+--------+ | 1 | 刘德华 | 1 | 张三 | 1 | | 1 | 刘德华 | 2 | 李四 | 1 | | 1 | 刘德华 | 3 | 王五 | 1 | | 2 | 张学友 | 4 | 赵六 | 2 | | 2 | 张学友 | 5 | 孙七 | 2 | +------+--------+----+------+--------+
2.3 全链接:返回符合条件的全部表的记录,没有与之匹配的,用null表示(结果是左链接和右链接的并集)
第一种写法:join
select t.*,s.* from teacher t full join student s on t.id=s.tea_id;
第二种写法:
select t.*,s.* from teacher t full outer join student s on t.id=s.tea_id;
3 交叉链接(结果是笛卡尔积)
select t.*,s.* from teacher t cross join student s;
等效于:
select t.*,s.* from teacher t,student s;
+----+--------+----+------+--------+ | id | name | id | name | tea_id | +----+--------+----+------+--------+ | 1 | 刘德华 | 1 | 张三 | 1 | | 2 | 张学友 | 1 | 张三 | 1 | | 4 | 黎明 | 1 | 张三 | 1 | | 1 | 刘德华 | 2 | 李四 | 1 | | 2 | 张学友 | 2 | 李四 | 1 | | 4 | 黎明 | 2 | 李四 | 1 | | 1 | 刘德华 | 3 | 王五 | 1 | | 2 | 张学友 | 3 | 王五 | 1 | | 4 | 黎明 | 3 | 王五 | 1 | | 1 | 刘德华 | 4 | 赵六 | 2 | | 2 | 张学友 | 4 | 赵六 | 2 | | 4 | 黎明 | 4 | 赵六 | 2 | | 1 | 刘德华 | 5 | 孙七 | 2 | | 2 | 张学友 | 5 | 孙七 | 2 | | 4 | 黎明 | 5 | 孙七 | 2 | +----+--------+----+------+--------+
5自链接:链接的表都是同一个表,一样能够由内链接,外链接各类组合
方式,按实际应用去组合。
SELECT a.*,b.* FROM table_1 a,table_1 b WHERE a.[name]=b.[name]
--链接的两表是同一个表,别称不同
SELECT a.*,b.* FROM table_1 a LEFT JOIN table_1 b ON a.[name]=b.[name]
--左链接写法
----------------------------------------------------------
易销宝(www.exiao800.com),企业专属的的定货平台,网上定货系统,加盟商定货软件,订单管理系统