56,678
社区成员
发帖
与我相关
我的任务
分享
一,直接方式
select b.bigclass,a.bigname from a,b where a.bigid=b.bigid
二, join方式
select b,bigclass, a.bigname from a right join b on a.bigid=b.bigid
mysql> explain EXTENDED select * from t2,t3 where t2.a=t3.a;
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
| 1 | SIMPLE | t2 | ALL | a,idx_a | NULL | NULL | NULL | 2 | 100.00 | |
| 1 | SIMPLE | t3 | ref | idx_a | idx_a | 5 | test.t2.a | 2 | 100.00 | Using where |
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
2 rows in set, 1 warning (0.02 sec)
mysql> show warnings;
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Level | Code | Message
|
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Note | 1003 | select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t3`.`c
` AS `c` from `test`.`t2` join `test`.`t3` where (`test`.`t2`.`a` = `test`.`t3`.`a`) |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> explain extended select * from t2 inner join t3 on t2.a=t3.a;
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
| 1 | SIMPLE | t2 | ALL | a,idx_a | NULL | NULL | NULL | 2 | 100.00 | |
| 1 | SIMPLE | t3 | ref | idx_a | idx_a | 5 | test.t2.a | 2 | 100.00 | Using where |
+----+-------------+-------+------+---------------+-------+---------+-----------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)
mysql> show warnings;
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Level | Code | Message
|
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
| Note | 1003 | select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c`,`test`.`t3`.`a` AS `a`,`test`.`t3`.`b` AS `b`,`test`.`t3`.`c
` AS `c` from `test`.`t2` join `test`.`t3` where (`test`.`t2`.`a` = `test`.`t3`.`a`) |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
select a.* from a,b where a.id=b.id;
select a.* from a inner join b on a.id=b.id;
mysql> use test;
Database changed
mysql> select * from t2;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 1 | 1 |
| 3 | 3 | 3 |
+---+------+------+
2 rows in set (0.10 sec)
mysql> select * from t3;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 888 |
| 2 | 2 | 888 |
| 2 | 2 | 888 |
| 2 | 2 | 888 |
| 2 | 2 | 888 |
| 2 | 2 | 888 |
| 0 | 1 | 888 |
| 0 | 1 | 888 |
| 1 | 1 | 888 |
| 1 | 1 | 888 |
| 1 | 1 | 888 |
| 1 | 1 | 888 |
+------+------+------+
12 rows in set (0.07 sec)
mysql> select * from t2,t3 where t2.a=t3.a;
+---+------+------+------+------+------+
| a | b | c | a | b | c |
+---+------+------+------+------+------+
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
+---+------+------+------+------+------+
5 rows in set (0.03 sec)
mysql> select * from t2 inner join t3 on t2.a=t3.a;
+---+------+------+------+------+------+
| a | b | c | a | b | c |
+---+------+------+------+------+------+
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
| 1 | 1 | 1 | 1 | 1 | 888 |
+---+------+------+------+------+------+
5 rows in set (0.00 sec)