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 ...
随机推荐
- linux的route
参考: https://blog.csdn.net/u011857683/article/details/83795435 老男孩: https://blog.51cto.com/oldboy/974 ...
- 阿里RDS
白名单设置: 创建高权限帐号:
- provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
通常情况下,要解决这个,你去SQL Server配置管理器(SSCM)和: [1]在SSCM中设置 [1.1]确保共享内存协议启用 [1.2]确保命名管道协议 [1.3]确保TCP / IP被启用,和 ...
- Linux、Aix(unix)、Oracle 银行外包开发运维常用命令
我一直是银行外包开发人员,常用的操作命令固然少不了,这是我一次自己边添加边使用的笔记.内容有点乱,希望可以帮到你. rm 文件或目录rm -f 文件或目录rm -rf * 跑路的时候用du -h 文件 ...
- 【LOJ】#3044. 「ZJOI2019」Minimax 搜索
LOJ#3044. 「ZJOI2019」Minimax 搜索 一个菜鸡的50pts暴力 设\(dp[u][j]\)表示\(u\)用\(j\)次操作能使得\(u\)的大小改变的方案数 设每个点的初始答案 ...
- mybatis-plus配置多数据源invalid bound statement (not found)
mybatis-plus配置多数据源invalid bound statement (not found) 错误原因 引入mybatis-plus应该使用的依赖如下,而不是mybatis <de ...
- Mysql中多表删除
1.从MySQL数据表A中把那些id值在数据表B里有匹配的记录全删除掉 DELETE t2 FROM A t1,B t2 WHERE t1.id = t2.id DELETE FROM t2 USIN ...
- C#常见面试题(一)——try-catch-finally-return
面试常会被问及try-catch-finally,现在做一下总结: 第一.不管有没有出现异常,finally块中代码都会执行. 第二.finally 代码块中不能有return. 第三.如果try 或 ...
- Elastic Search中mapping的问题
Mapping在ES中是非常重要的一个概念.决定了一个index中的field使用什么数据格式存储,使用什么分词器解析,是否有子字段,是否需要copy to其他字段等.Mapping决定了index中 ...
- JavaScript设计模式(单例模式)
单例模式是一种简单但非常实用的模式,特别是惰性单例技术,在合适的时候才创建对象,并且只创建唯一的一个.下面我们来逐步了解单例模式的用法. 一.简版单例模式: var Singleton = funct ...