实验中涉及到两个表的结构:
mysql> show create table lee;
+-------+------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------+
| lee | CREATE TABLE `lee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(5) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1200 DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table lee1;
+-------+------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------+
| lee1 | CREATE TABLE `lee1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`gameid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=201 DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------+
1 row in set (0.00 sec)
一、连接查询类型:
1、内连接查询:
select lee.id , lee1.gameid from lee inner join lee1 using(id) where lee.id = 100; 或者
select lee.id, lee1.gameid from lee inner join lee1 on lee.id=lee1.id where lee.id = 100;
+-----+---------+
| id | gameid |
+-----+---------+
| 100 | 1000100 |
+-----+---------+
2、左(右)外连接查询:
mysql> select * from lee left join lee1 on lee.id = lee1.id where lee.id = 1197;
+------+-------+------+--------+
| id | name | id | gameid |
+------+-------+------+--------+
| 1197 | lee11 | NULL | NULL |
+------+-------+------+--------+
1 row in set (0.00 sec)
右外连接与上述类似,此处略过
二、左外连接、右外连接、内连接区别:
lee lee1 表初始内容
mysql> select * from lee join lee1 on lee.id = lee1.id limit 10;
+----+-------+------+--------+
| id | name | id | gameid |
+----+-------+------+--------+
| 1 | lee1 | 1 | 10001 |
| 2 | lee2 | 2 | 10002 |
| 3 | lee3 | 3 | 10003 |
| 4 | lee4 | 4 | 10004 |
| 5 | lee5 | 5 | 10005 |
| 6 | lee6 | 6 | 10006 |
| 7 | lee7 | 7 | 10007 |
| 8 | lee8 | 8 | 10008 |
| 9 | lee9 | 9 | 10009 |
| 10 | lee10 | 10 | 100010 |
+----+-------+------+--------+
10 rows in set (0.00 sec)
分别删除两个表各一条记录
mysql> delete from lee1 where id = 6;
Query OK, 1 row affected (0.00 sec)
mysql> delete from lee where id = 7;
Query OK, 1 row affected (0.00 sec)
mysql> select * from lee left join lee1 on lee.id = lee1.id limit 10;
+----+-------+------+--------+
| id | name | id | gameid |
+----+-------+------+--------+
| 1 | lee1 | 1 | 10001 |
| 2 | lee2 | 2 | 10002 |
| 3 | lee3 | 3 | 10003 |
| 4 | lee4 | 4 | 10004 |
| 5 | lee5 | 5 | 10005 |
| 6 | lee6 | NULL | NULL |
| 8 | lee8 | 8 | 10008 |
| 9 | lee9 | 9 | 10009 |
| 10 | lee10 | 10 | 100010 |
| 11 | lee11 | 11 | 100011 |
+----+-------+------+--------+
说明:左外连接是join左侧的表为主,如果join右侧的表不符合则用NULL填充
mysql> select * from lee right join lee1 on lee.id = lee1.id limit 10;
+------+-------+----+--------+
| id | name | id | gameid |
+------+-------+----+--------+
| 1 | lee1 | 1 | 10001 |
| 2 | lee2 | 2 | 10002 |
| 3 | lee3 | 3 | 10003 |
| 4 | lee4 | 4 | 10004 |
| 5 | lee5 | 5 | 10005 |
| NULL | NULL | 7 | 10007 |
| 8 | lee8 | 8 | 10008 |
| 9 | lee9 | 9 | 10009 |
| 10 | lee10 | 10 | 100010 |
| 11 | lee11 | 11 | 100011 |
+------+-------+----+--------+
10 rows in set (0.00 sec)
说明:同理,右外连接是以join右侧的表为主,如果join左侧的表不符合则用NULL填充
mysql> select * from lee inner join lee1 on lee.id = lee1.id limit 10;
+----+-------+----+--------+
| id | name | id | gameid |
+----+-------+----+--------+
| 1 | lee1 | 1 | 10001 |
| 2 | lee2 | 2 | 10002 |
| 3 | lee3 | 3 | 10003 |
| 4 | lee4 | 4 | 10004 |
| 5 | lee5 | 5 | 10005 |
| 8 | lee8 | 8 | 10008 |
| 9 | lee9 | 9 | 10009 |
| 10 | lee10 | 10 | 100010 |
| 11 | lee11 | 11 | 100011 |
| 12 | lee12 | 12 | 100012 |
+----+-------+----+--------+
10 rows in set (0.00 sec)
说明:可以看出inner连接是取左右外连接的并集。
--转自