Home >Database >Mysql Tutorial >MySQL一些查询的解决方法_MySQL

MySQL一些查询的解决方法_MySQL

WBOY
WBOYOriginal
2016-06-01 13:31:301090browse

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
Statement:
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