Rumah  >  Artikel  >  pangkalan data  >  MySQL一些查询的解决方法_MySQL

MySQL一些查询的解决方法_MySQL

WBOY
WBOYasal
2016-06-01 13:31:301029semak imbas

bitsCN.com

MySQL一些查询的解决方法

 

Mysql 计算多个字段的值

原始数据库如下,所有的字段均为int 类型

 

+----+---------+---------+| id | column1 | column2 |+----+---------+---------+|  1 |       1 |       2 ||  2 |       3 |       4 ||  3 |       5 |       6 |+----+---------+---------+

 

希望的输出 result 的值为 

column1 * column2+----+--------+| id | Result |+----+--------+|  1 |      2 ||  2 |     12 ||  3 |     30 |+----+--------+

 

使用的SQL 语句为

 

SELECT id, (column1 * column2) AS Result FROM table;

 

几点注意:

 

Mysql 支持常用的所有的算数运算符 +, -, *, /, %, p(整除), -(负号)

column1 * column2 这个表达式不能写成 'column1' * 'column2' 或 'column1 * column2'

Mysql 将多个字段值作为文本连接

还是上的数据库,这次希望输出为字符串形式的 column1 * column2,即:

 

+----+--------+| id | Result |+----+--------+|  1 | 1 * 2  ||  2 | 3 * 4  ||  3 | 5 * 6  |+----+--------+

 

使用如下的SQL 语句实现

 

SELECT id, CONCAT(column1, ' * ', column2) AS Result  FROM table;

 

以后添加

bitsCN.com
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn