MySQL从无关联的两个或多个表中查询数据,列转行等
2017-12-20 16:57
337 查看
背景需求:用一条sql查询没有任何关联的两张表,并且查询中涉及到了聚合函数,查询结果需要作为一条数据(列转行)。
两张没有关联的表放到一个结果集中,此处考虑使用union all,
select count(1) bcrzc,0 bsjgj,opt_time from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and is_delete = 0
union all
select 0 bcrzc,count(1) bsjgj,op_date opt_time from til_inspection_rec where brand = '保时捷' and step = '港检' and is_deleted = 1
结果如下:
![](https://img-blog.csdn.net/20171220165907001)
简单的union all查出来是以不同数据行的形式显示,需要列转行,外边就加了一层查询
select sum(a.bcrzc) bcrzc, sum(a.bsjgj) bsjgj from(
select count(1) bcrzc,0 bsjgj,opt_time from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and is_delete = 0
union all
select 0 bcrzc,count(1) bsjgj,op_date opt_time from til_inspection_rec where brand = '保时捷' and step = '港检' and is_deleted = 1) a
where a.opt_time >= '2017-01-01' and a.opt_time <= '2017-11-30'
![](https://img-blog.csdn.net/20171220170114233)
第二种查询方法:
select (select count(1) from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and opt_time >= '2017-01-01' and opt_time <= '2017-11-30') AAA
,(select count(1) from til_inspection_rec where brand = '保时捷' and step = '港检' and op_date >= '2017-01-01' and op_date <= '2017-11-30') BBB
![](https://img-blog.csdn.net/20171220170136426)
虽然第二种查询看起来稍微简单容易理解一些,但是很明显效率没有第一种高,猜想是由于where后的时间条件重复校验导致的。
两张没有关联的表放到一个结果集中,此处考虑使用union all,
select count(1) bcrzc,0 bsjgj,opt_time from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and is_delete = 0
union all
select 0 bcrzc,count(1) bsjgj,op_date opt_time from til_inspection_rec where brand = '保时捷' and step = '港检' and is_deleted = 1
结果如下:
简单的union all查出来是以不同数据行的形式显示,需要列转行,外边就加了一层查询
select sum(a.bcrzc) bcrzc, sum(a.bsjgj) bsjgj from(
select count(1) bcrzc,0 bsjgj,opt_time from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and is_delete = 0
union all
select 0 bcrzc,count(1) bsjgj,op_date opt_time from til_inspection_rec where brand = '保时捷' and step = '港检' and is_deleted = 1) a
where a.opt_time >= '2017-01-01' and a.opt_time <= '2017-11-30'
第二种查询方法:
select (select count(1) from t_manual_entry_workload a where opt_type = 'CIQ入暂存' and opt_time >= '2017-01-01' and opt_time <= '2017-11-30') AAA
,(select count(1) from til_inspection_rec where brand = '保时捷' and step = '港检' and op_date >= '2017-01-01' and op_date <= '2017-11-30') BBB
虽然第二种查询看起来稍微简单容易理解一些,但是很明显效率没有第一种高,猜想是由于where后的时间条件重复校验导致的。
相关文章推荐
- 两个不同数据库之间的两个表如何关联起来查询数据
- Linq从两个edmx(两个数据库)中选取数据进行查找出现错误解决 (指定的 LINQ 表达式包含对与不同上下文关联的查询)
- mysql 两个关联表如何更新其中一个表的数据
- mysql 连接查询指的是将两张表或多张表关联到一起进行查询,获取一个表的行与另一个表的行匹配的数据。常见的连接查询包括内连接(等值连接)、左(外)连接、右(外)连接和交叉连接(完全连接)等
- mysql 中两个表数据怎么关联显示
- mysql 查询关联更新数据
- 两个不同数据库之间的两个表如何关联起来查询数据
- Mysql 如何查询两个时间段之间的数据?
- mysql关联查询怎么在查询软件中一下就能查询到所有的关联数据,修改的时候也方便,对比数据
- mysql 根据关联查询结果 修改 或者 添加 数据
- mysql:如何合并两个查询的结果集的数据
- mysql多表关联查询进行数据修改
- Mysql联接查询-JOIN-从多个关联的表中查询数据
- 两个不同数据库之间的两个表如何关联起来查询数据
- MySql同时查询三个不关联的表,同时获取到三个表的数据,并按照某一字段排序
- mysql 2个数据表关联查询
- MYSQL group_concat()查询关联数据一条显示
- mysql 查询两个表相同数据 全部数据 查询一个表中另外一个表不存在的数据
- mysql update 两个表关联{两个关联表如何更新其中一个表的数据}
- 在mysql中查询不重复的数据并且该数据不为空