MySQL query Solution

Source: Internet
Author: User

MySQL query solution: Mysql calculates the values of multiple fields. The original database is as follows. All fields are of the int type.

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

 

The expected output result value is
column1 * column2+----+--------+| id | Result |+----+--------+|  1 |      2 ||  2 |     12 ||  3 |     30 |+----+--------+

 

The SQL statement used is
SELECT id, (column1 * column2) AS Result FROM table;

 

Note: Mysql supports all commonly used Arithmetic Operators +,-, *,/, %, DIV (Division),-(negative) the column1 * column2 expression cannot be written as 'column1 '* 'column2' or 'column1 * column2 '. Mysql uses multiple field values as text links or databases, this time, you want to output column1 * column2 in the string format, that is:
+----+--------+| id | Result |+----+--------+|  1 | 1 * 2  ||  2 | 3 * 4  ||  3 | 5 * 6  |+----+--------+

 

Use the following SQL statement
SELECT id, CONCAT(column1, ' * ', column2) AS Result  FROM table;

 

Add later

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.