Home > php教程 > php手册 > body text

php mysql 求和语句

WBOY
Release: 2016-05-24 18:30:52
Original
1884 people have browsed it

自动计算二个字段的值赋给另一字段sql:

$sql ="update table set sum = mathe +language";
Copy after login

求一个字段列的数据总和:

$sql = "select sum(sum) from tables";
Copy after login

下们我们来看个实例,创建表代码如下:

CREATE TABLE `cc`.`sumtable` ( 
`id` INT( 4 ) NOT NULL , 
`a1` INT( 4 ) NOT NULL , 
`a2` INT( 4 ) NOT NULL , 
`summ` INT( 8 ) NOT NULL  
) ENGINE = InnoDB
Copy after login

插入记录,代码如下:

INSERT INTO `cc`.`sumtable` ( 
`id` , 
`a1` , 
`a2` , 
`summ`  
) 
VALUES ( 
'1', '5', '5', '0' 
), ( 
'2', '6', '5', '0' 
); 
*/
Copy after login
 
//自动计算二个字段的值赋给另一字段sql 
update sumtable set summ = a1+a2 
//求和 
SELECT sum( summ ) FROM sumtable  
sum(summ)   
21
Copy after login

                     


文章地址:

转载随意^^请带上本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!