sql求和查询

如图,想要统计name为张三和李四的time为20180919的d1、d2、d3的和,sql应该怎么怎么写,请大神指点

1、创建三张测试表,

create table pw_memberinfo(uid int, name varchar(20));

create table pw_members(companyid int, uid int);

create table pw_memberdata(uid int, deposit int, ddeposit int, money int);

2、三张表,分别插入测试数据,

insert into pw_members values(1, 1);

insert into pw_members values(1, 2);

insert into pw_members values(1, 3);

insert into pw_memberinfo values(1, 'name_1');

insert into pw_memberinfo values(2, 'name_2');

insert into pw_memberinfo values(3, 'name_3');

insert into pw_memberdata values(1,30,50,150);

insert into pw_memberdata values(2,77,50,12);

insert into pw_memberdata values(3,44,50,82);

3、查看pw_memberdata表中的记录,select * from pw_memberdata t,

4、编写sql语句,

select * from (SELECT i.uid, sum(deposit+ddeposit+money) as allmoney 

FROM pw_memberinfo i 

LEFT JOIN pw_members m ON m.uid=i.uid 

LEFT JOIN pw_memberdata d ON i.uid=d.uid 

group by i.uid

) t where allmoney>200

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-09-20
select sum(d1) as d1sum, sum(d2) as d2sum, sum(d3) as d3sum from your_table_name where name in ('张三', '李四') and time = '20180919';

本回答被提问者和网友采纳
相似回答