永发信息网

一句sql语句查询 差

答案:4  悬赏:50  手机版
解决时间 2021-01-29 19:57
  • 提问者网友:無理詩人
  • 2021-01-29 15:21
sql语句 表结构:
value type
23 1
42 1
12 2
21 2
如何查询出当type=1的value的和(即23+42)与当type=2的和(12+21)的差
(即(23+42) - (12+21))的sql语句 当然实际数据不止这么点
我最后只要最后的这个 差(即32)?????求大神解答
type只有1 和 2 value会有很多
最佳答案
  • 五星知识达人网友:傲气稳了全场
  • 2021-01-29 16:43
select sum(Typ1Sum)-sum(Typ2Sum) as 差 from(

select case type when '1'then sum(value) end as Typ1Sum,
case type when '2'then sum(value) end as Typ2Sum
from table1 group by type
)M
全部回答
  • 1楼网友:不甚了了
  • 2021-01-29 18:34
select a.value-b.value from (select '1' as no, sum(value) as value from table where type = 1) a,  (select '1' as no, sum(value) as value from table where type = 2) b where a.no = b.no试一下
  • 2楼网友:舊物识亽
  • 2021-01-29 17:47
你好朋友case type when '2'then sum(value) end as Typ2Sum from table1 group by typesql语句 表结构: value type 23 1 42 1 12 2 21 2 如何查询出当type=1的value的和(即23+42)与当type=2的和(12+21)的差 (即(23+42) - (12+21))的sql语句 当然实际数据不止这么点 我最后只要最后的...展开 type只有1 和 2 value会有很多
  • 3楼网友:底特律间谍
  • 2021-01-29 17:19

正确

以这句select distinct depnum from user where uid=any(select distinct userid from book)

比如 user 表有数据 (uid,depnum){(1,'aa'),(3,'bb'),(2,'cc')}这样三个数据

book id{1,2,1,}这个三个数据,select distinct userid from book取出的数据就是1,2

any表示只要有uid与之进行比较一个相同就取出 结果(1,'aa')(2,'cc')

以此类推得到最后结果

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯