使用聚和函数 sum() 求和
select sum(money) from record t where t.name = ?
注意:t 是一个表的别名,比如 t.name 就是取 record 表中的name,使用 record.name 也可以,但就是太长了,所以起一个别名比较方便
扩展资料:
聚集函数是 AVG、COUNT、MAX、MIN 和 SUM,以下示例中描述了聚集函数的语法
aggregation-function ( [ ALL | DISTINCT ] expression )
或:COUNT( [ ALL | DISTINCT ] identification-variable )
或:COUNT( * )
在应用函数之前,DISTINCT 选项消除重复值。
参考资料来源:百度百科-聚集函数
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
您好,是这样的:declare @str varchar(100)set @str='U8中的销售发票、采购发票 关键字' --要搜索的字符串 declare @s varchar(8000)declare tb cursor local forselect s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'') print ''所在的表及字段: ['+b.name+'].['+a.name+']'''from syscolumns a join sysobjects b on a.id=b.idwhere b.xtype='U' and a.status>=0 and a.xusertype in(175,239,231,167)open tbfetch next from tb into @swhile @@fetch_status=0begin exec(@s) fetch next from tb into @sendclose tbdeallocate tb欢迎分享,转载请注明来源:内存溢出
评论列表(0条)