sql 有重复字段的多个表查询时,数据被覆盖怎么解决?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 有重复字段的多个表查询时,数据被覆盖怎么解决?相关的知识,希望对你有一定的参考价值。
select a.no
,sum(case when a.index = 1 then a.value else 0 end)/count(distinct a.calcdate) as aa
,sum(case when c.index = 5 then c.value else 0 end)/count(distinct c.calcdate) as cc
from A a join C c on a.bid = c.bid
group by a.no
表A和表C字段相同,都有no,index,value,calcdate,现在运行后c的值会覆盖a的值运算
求不会覆盖的方法
,sum(case when a.index = 1 then a.value else 0 end)/count(distinct a.calcdate) as aa
from A
group by a.no
union all
select c.no
,sum(case when c.index = 5 then c.value else 0 end)/count(distinct c.calcdate) as cc
from C
group by C.no追问
运行后没有cc那列,只有no和aa
追答select no
,sum(case when index = 1 then value else 0 end)/count(distinct calcdate) as aa
from A
group by no
union all
select no
,sum(case when index = 5 then value else 0 end)/count(distinct calcdate) as aa
from C
group by no
这样看下数据对不对,上下查询的结果都放同一个字段里面
不行的
追答我后面这个代码运行完是怎么样的,截图看下
MySQL查询时,怎么排除某个字段查询
参考技术A select [需要的字段] from [表] where [查询条件]需要的字段这边填入需要的字段。如果需要所有的字段则填入*
以上是关于sql 有重复字段的多个表查询时,数据被覆盖怎么解决?的主要内容,如果未能解决你的问题,请参考以下文章