select * from a,b探讨
Posted zzliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了select * from a,b探讨相关的知识,希望对你有一定的参考价值。
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探讨的主要内容,如果未能解决你的问题,请参考以下文章
为啥“SELECT DISTINCT a, b FROM...”返回的记录少于“SELECT DISTINCT A + '|' + B 来自...”?
sql语句 select a+b from c b有时是NULL 如何修改语句,在b不等于null时,执行select a+b from c
MySQL SELECT x FROM a WHERE NOT IN (SELECT x FROM b) - 意外结果
SQL 语句 select sum(a) from table1 where b=3