mysql 求和语句

mysql 求和语句,第1张

使用聚和函数 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 选项消除重复值。

参考资料来源:百度百科-聚集函数

CREATE DEFINER=`root`@`localhost` PROCEDURE `aa`(IN `supplierId` varchar(50),IN `cityId` varchar(50))

BEGIN

#declare str varchar(8000)

declare pos INT

declare lastpos INT

declare startindex INT

declare spid VARCHAR(50)

set @str = 'select '

set pos = LOCATE(',',supplierId)

set startindex = 1

set lastpos = 1

while (pos<>0) do

set lastpos = pos

set spid = SUBSTRING(supplierId,startindex,pos-1)

set @str = concat(@str,'(select (case when sum(dispatchingAmount) is null then (select sum(dispatchingAmount) from tb_dispatching_amount where cityId is null and supplierId=',spid,' )else sum(dispatchingAmount) end ) as dispatchingAmount from tb_dispatching_amount as a where cityId=''',cityId,''' and supplierId = ',spid,')+')

set pos = LOCATE(',',supplierId,pos+1)

set startindex = pos

end while

set spid = SUBSTRING(supplierId,lastpos+1)

set @str = concat(@str,'(select (case when sum(dispatchingAmount) is null then (select sum(dispatchingAmount) from tb_dispatching_amount where cityId is null and supplierId=',spid,' )else sum(dispatchingAmount) end ) as dispatchingAmount from tb_dispatching_amount as a where cityId=''',cityId,''' and supplierId = ',spid,')+')

set @str = SUBSTRING(@str,1,LENGTH(@str)-1)

prepare stmt1 from @str

execute stmt1

deallocate prepare stmt1

#select @str

END

分别求出d01、d02、d03......d31列的和;

SELECT count(d01),count(d02),count(d03).....count(d31) FROM m201201

分别求出3006、3008、3010、3016、3034每一行中d01——d31之间记录的和

SELECT (d01+d02+d03+....+d31) as d_all FROM m201201 WHERE name IN('3006','3008','3010','3016','3034')

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。

MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。

MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。

由于其社区版的性能卓越,搭配 PHP 和 Apache 可组成良好的开发环境。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8554779.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-17
下一篇2023-04-17

发表评论

登录后才能评论

评论列表(0条)

    保存