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 allmoney200
$sql ="select sum(dingdan_price) from dingdan";
$re = mysql_query($sql);
list($sum)=mysql_fetch_row($re);
mysql_free_result($re);
echo "总和=$sum";
SELECT SUM(CASE `支付状态` WHEN 2 THEN `支付金额` ELSE 0 END)
,SUM(CASE `支付状态` WHEN 2 THEN 1 ELSE 0 END)
,SUM(CASE `支付状态` WHEN 1 THEN `支付金额` ELSE 0 END)
,SUM(CASE `支付状态` WHEN 1 THEN 1 ELSE 0 END)
FROM `订单`
这个SQL的结果是这个样子,4个数字表示成功金额、成功笔数、失败金额、失败笔数
123.45 11 2345.67 222
也可以用这样的简单SQL语句统计:
SELECT `支付状态`, COUNT(*), SUM(`支付金额`) FROM `订单`
结果为两行,分别是成功的笔数和金额、失败的比如和金额,例如是这样:
1 11 123.45
2 22 2345.67
写一条sql语句就能解决了:
select sum(贷款总金额字段名) from tablename where 贷款部门字段='张西堡信用社';