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探讨的更多相关文章
- JAVA框架 Spring 和Mybatis整合(动态代理)
一.使用传统方式的dao的书写方式,不建议.目前采用的是动态代理的方式交给mybatis进行处理. 首先回顾下动态代理要求: 1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法 ...
- 最全的ORACLE-SQL笔记
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...
- Matplotlib数据可视化(6):饼图与箱线图
In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...
- 问题分析探讨 --> 大约有700W数据的表,把当天的10W数据select导入新表,整个原来的表就锁死
Sun shine 16:15:55 帅哥 我有个手机表 大约有700百数据,,每天新增 大约五万,并且新也有update 大约10万 然后 我每晚 把当天的数据select 导入一个新表中的时 ...
- 探讨SELECT语句的元数据&动态取样&读一致性导致的一致性读和递归操作
前几天,论坛上的同行在讨论SELECT语句的元数据,动态取样和读一致性导致的一致性读和递归问题,今天有时间,就试着进行了测试,本人测试环境如下: win7_64+Oracle11.2.0.4_64 那 ...
- Repository 仓储,你的归宿究竟在哪?(三)-SELECT 某某某。。。
写在前面 首先,本篇博文主要包含两个主题: 领域服务中使用仓储 SELECT 某某某(有点晕?请看下面.) 上一篇:Repository 仓储,你的归宿究竟在哪?(二)-这样的应用层代码,你能接受吗? ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。
前言: 都说程序员有三宝:人傻,钱多,死得早.博主身边的程序“猿”一大半应了这三宝,这从侧面说明了一个问题,只有理性是过不好日子的.朋友们应该把工作与生活分开,让生活变得感性,让工作变得理性,两者相提 ...
- 使select文本框可编辑可选择(jQuery插件)
最近做项目中用到了这个插件,正好分享下. 1. 需要用的js包点击下载,在项目中引入该js. <script src="${pageContext.request.contextPa ...
随机推荐
- oracle导出空表
1.先查询数据库空表 select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0 ...
- [LGP4707] 重返现世
世界是物质的,物质是运动的,运动是有规律的,规律是可以被认识的. 关于期望意义下min-max容斥,我们认为每个事件的时间来认识事件,max/min S表示集合S中所有时间最后/最前出现的事件,E(m ...
- k8s基础知识
k8s部署到云计算上的优势 传统模式: 在k8s外部部署一个外置调度器,请求先到物理node节点ip地址,由物理服务地址再转发代理至service地址,如果node节点也是私网地址呢,就需要再外部部署 ...
- python 初始化__init__()
init()方法的重要性体现在两点. 1.初始化既是对象生命周期的开始,也是非常重要的一个步骤,每个对象都必须正确的执行了初始化才能够正常的工作 2.__init__()方法的参数可以多种形式来完成赋 ...
- vue 模拟测试数据构建
等价=====================================
- 【原创】大叔经验分享(67)spring boot启动报错
spring boot 启动报错: Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback Logg ...
- Pytorch中nn.Dropout2d的作用
Pytorch中nn.Dropout2d的作用 首先,关于Dropout方法,这篇博文有详细的介绍.简单来说, 我们在前向传播的时候,让某个神经元的激活值以一定的概率p停止工作,这样可以使模型泛化性更 ...
- 使用vue-echarts,需要按需引入
引入缺失报错: Error in callback for watcher "options": "Error: Component series.bar not exi ...
- linux下内存检测工具的使用和对比
linux背后隐藏着各种丰富的工具,学会这些工具,让这些工具更好地服务于我们的项目开发,不仅可以提高工作的效率,而且可以增强个人技术力. 参考:http://blog.chinaunix.net/ui ...
- 使用HandyJSON导致的内存泄漏问题相关解决方法
在移动开发中,与服务器打交道是不可避免的,从服务器拿到的接口数据最终都会被我们解析成模型,现在比较常见的数据传输格式是json格式,对json格式的解析可以使用原生的解析方式,也可以使用第三方的,我们 ...