SQL 求帮助。。。既包含又包含

sky191122 2018-09-23 03:55:48


想求出只要含有1和2状态用户的所有的金额。
结果是
001 640
002 2300
...全文
1120 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_42686440 2018-10-10
  • 打赏
  • 举报
回复
weixin_42686440 2018-10-10
  • 打赏
  • 举报
回复
weixin_42686440 2018-10-10
  • 打赏
  • 举报
回复
select `状态`,SUM(`金额`) as num FROM 表名 WHERE `状态` = '001' or `状态`='002' group by `状态`
yeminqiang_2008 2018-09-29
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('test') IS NOT NULL DROP TABLE test
create table test(u varchar(10), status int, num int)
go
insert into test values
('001', 1, 240),
('002', 2, 1000),
('001', 3, 400),
('002', 4, 1300),
('003', 4, 60),
('004', 5, 120),
('005', 3, 700)
go
select a.U,SUM(num) as num from test as a,
(select U from test where status =1 or status =2) as b
where a.u=b.u group by a.u



(7 行受影响)
U num
---------- -----------
001 640
002 2300

(2 行受影响)
ZL小屁孩 2018-09-28
  • 打赏
  • 举报
回复
使用in和like字段不行吗
诸葛睫1874 2018-09-28
  • 打赏
  • 举报
回复
select 用户,sum(金额)
from table
where 用户 in(
select 用户 from table where 状态 in ('1','2')
)
group by 用户
zjcxc 2018-09-28
  • 打赏
  • 举报
回复
from tb a, tb b where a.用户=b.用户 and a.status=1 and b.status=2
咸鱼小码 2018-09-28
  • 打赏
  • 举报
回复
select 用户,sum(金额) from 表 where 用户 in(
select 用户 from 表 where 状态 in(1,2)) group by 用户
卖水果的net 2018-09-23
  • 打赏
  • 举报
回复

create table test(u varchar(10), status int, num int)
go
insert into test values
('001', 1, 240),
('002', 2, 1000),
('001', 3, 400),
('002', 4, 1300),
('003', 4, 60),
('004', 5, 120),
('005', 3, 700)
go
select u, SUM(num) num from test
where exists(select * from test x where x.status in(1,2) and test.u = x.u)
group by u
go
drop table test
go


(7 行受影响)
u          num
---------- -----------
001        640
002        2300

(2 行受影响)


刘熙 2018-09-23
  • 打赏
  • 举报
回复
where 状态 = 1 or 状态 = 2

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧