码迷,mamicode.com
首页 > 其他好文 > 详细

select * from a,b探讨

时间:2019-08-17 22:05:59      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:使用   nod   可见   代码   har   null   临时表   select   验证   

select * from a,b探讨

今天看同事代码里使用了select * from a,b where a.id=b.id,而我平时都是使用select * from a inner join b where a.id=b.id,于是查了下,发现:

1)单纯的select * from a,b是笛卡尔乘积

2)select * from a,b where a.id=b.id相当于inner join


验证

1)创建两张表

create table userinfo(
        uid int(10) not null default 0,
        report_id int(10) not null default 0,
        primary key(uid)
       ) engine=innodb default charset=utf8;

create table report(
       report_id int(10) not null default 0,
       description varchar(255) default '',
       primary key(report_id)
       ) engine=innodb default charset=utf8;



2)插入测试数据

insert into userinfo values(1,1),(2,1),(3,2),(4,6);
insert into report values(1,'第一条'),(2,'第二条'),(3,'第三条');
mysql> select * from userinfo;
+-----+-----------+
| uid | report_id |
+-----+-----------+
|   1 |         1 |
|   2 |         1 |
|   3 |         2 |
|   4 |         6 |
+-----+-----------+
4 rows in set (0.00 sec)

mysql> select * from report;
+-----------+-------------+
| report_id | description |
+-----------+-------------+
|         1 | 第一条      |
|         2 | 第二条      |
|         3 | 第三条      |
+-----------+-------------+
3 rows in set (0.00 sec)


3)验证

单独的select * from a,b

select * from userinfo,report

结果

mysql> select * from userinfo,report;
+-----+-----------+-----------+-------------+
| uid | report_id | report_id | description |
+-----+-----------+-----------+-------------+
|   1 |         1 |         1 | 第一条      |
|   1 |         1 |         2 | 第二条      |
|   1 |         1 |         3 | 第三条      |
|   2 |         1 |         1 | 第一条      |
|   2 |         1 |         2 | 第二条      |
|   2 |         1 |         3 | 第三条      |
|   3 |         2 |         1 | 第一条      |
|   3 |         2 |         2 | 第二条      |
|   3 |         2 |         3 | 第三条      |
|   4 |         6 |         1 | 第一条      |
|   4 |         6 |         2 | 第二条      |
|   4 |         6 |         3 | 第三条      |
+-----+-----------+-----------+-------------+
12 rows in set (0.00 sec)

可见select * from a,b是笛卡儿积


再来验证select * from a,b where a.id=b.id

mysql> select * from userinfo,report where userinfo.report_id=report.report_id;
+-----+-----------+-----------+-------------+
| uid | report_id | report_id | description |
+-----+-----------+-----------+-------------+
|   1 |         1 |         1 | 第一条      |
|   2 |         1 |         1 | 第一条      |
|   3 |         2 |         2 | 第二条      |
+-----+-----------+-----------+-------------+
3 rows in set (0.00 sec)



inner join

mysql> select * from userinfo inner join report where userinfo.report_id=report.report_id;
+-----+-----------+-----------+-------------+
| uid | report_id | report_id | description |
+-----+-----------+-----------+-------------+
|   1 |         1 |         1 | 第一条      |
|   2 |         1 |         1 | 第一条      |
|   3 |         2 |         2 | 第二条      |
+-----+-----------+-----------+-------------+
3 rows in set (0.00 sec)

mysql> select * from userinfo inner join report on userinfo.report_id=report.report_id;
+-----+-----------+-----------+-------------+
| uid | report_id | report_id | description |
+-----+-----------+-----------+-------------+
|   1 |         1 |         1 | 第一条      |
|   2 |         1 |         1 | 第一条      |
|   3 |         2 |         2 | 第二条      |
+-----+-----------+-----------+-------------+
3 rows in set (0.00 sec)

可见是select * from a,b where a.id=b.id只是把笛卡尔积做了一层过滤,结果与inner join相同

补充:inner join是先生成一个临时表,然后使用on条件筛选

注:以上结论只在mysql 5.7验证过,其他数据库不一定成立

select * from a,b探讨

标签:使用   nod   可见   代码   har   null   临时表   select   验证   

原文地址:https://www.cnblogs.com/zzliu/p/11370272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!