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

MySQL一些查询的解决方法_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:31:30
Original
1135 people have browsed it

bitsCN.com

MySQL一些查询的解决方法

 

Mysql 计算多个字段的值

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

 

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

希望的输出 result 的值为

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

使用的SQL 语句为

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

几点注意:

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

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

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

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

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

使用如下的SQL 语句实现

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

 

以后添加

bitsCN.com
Related labels:
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