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 ...
随机推荐
- Node流操作(启动器forever)
详情: https://www.cnblogs.com/lalalagq/p/9749680.html 流:读取流,写入流,双向读写流. 读写流——压缩.加密 数据库不能直接接受流 sf.readFi ...
- hdoj4812 D Tree(点分治)
题目链接:https://vjudge.net/problem/HDU-4812 题意:给定一颗带点权的树,求是否存在一条路经的上点的权值积取模后等于k,如果存在多组点对,输出字典序最小的. 思路: ...
- [转帖]公钥基础设施(PKI)/CFSSL证书生成工具的使用
公钥基础设施(PKI)/CFSSL证书生成工具的使用 weilovepan520关注1人评论84344人阅读2018-05-26 12:22:20 https://blog.51cto.com/liu ...
- 【转帖】Linux的桌面环境gnome、kde、xfce、lxde 等等使用比较
Linux的桌面环境gnome.kde.xfce.lxde 等等使用比较 https://www.cnblogs.com/chenmingjun/p/8506995.html 文章目录 图形界面架起用 ...
- spring boot 项目开发常用目录结构
在spring boot开发中一些常用的目录划分 转载自https://blog.csdn.net/Auntvt/article/details/80381756: 一.代码层结构 根目录:net.c ...
- ARTS 第一周打卡
Algorithm : 做一个 leetcode 的算法题 1.只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算 ...
- k8s之statefulSet-有状态应用副本集控制器
1.概述 无状态应用更关注群体,任何一个成员都可以被取代,有状态应用关注的是个体.用deployment控制器管理的nginx.myapp等都属于无状态应用,像mysql.redis.zookeepe ...
- SpringBoot整合MyBatis的分页插件PageHelper
1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...
- es6字符串扩展(+模板字符串拼接)
includes() 判断字符串中是否包含指定的字串(有的话返回true,否则返回false) console.log('hello world'.includes('world' , 7)); // ...
- 2.Struts2-Action
struts.xml文件中 action 标签中几个属性的作用 1.name:为action命名,输入url访问时,需要带上action的name,通过name知道访问哪个action(通过class ...