select * from a,b
最编程
2024-03-10 15:47:37
...
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验证过,其他数据库不一定成立
推荐阅读
-
如何从 bilibili(B 站)下载音频和视频
-
HEVC:I 帧、P 帧和 B 帧
-
SQLite 子查询示例 - 在 SELECT 语句中使用子查询
-
如何开设 Goodies Select 联盟?如何提高快车道商家的人气?
-
P6622[2020年省级遴选联考A/B卷]信号学解题思路
-
1553B FPGA 逻辑:BU61580 的替代产品,代码可移植,具有完整的 RT/BC/BM 功能,附带验证板设计、PCB 和逻辑验证代码
-
jsPDF + html2canvas + Vue3 + ts 项目,逐页导出当前页面为 PDF,A 页导出 B 页内容为 PDF,隐藏导出按钮等多余元素。
-
HTML B 粗体标签
-
B0102 Linux centos7.9 操作系统的安装和维护
-
Linux-Orient 项目 B 站的二次元世界一瞥