Timmy February 2016

Multiply values extracted with a select in SQL

Using SQL I'd like to multiply values extracted using a select: Here an example:

SELECT Number1, Number2
FROM myTable
WHERE PrimaryKey = MyPrimaryKey

And the result for example is: 3, 4.

Instead what I'd like to obtain is 12.

So it's possible to make something like this?

SELECT Number1 * Number2
FROM myTable
WHERE PrimaryKey = MyPrimaryKey

Answers


Mukesh Keshu February 2016

Yes, it is possible.

SELECT Number1 * Number2
FROM myTable
WHERE PrimaryKey = MyPrimaryKey


gimbel0893 February 2016

Yeah you got it

mysql> show create table numbers\G
*************************** 1. row ***************************
       Table: numbers
Create Table: CREATE TABLE numbers (
  'id' int(11) NOT NULL,
  'num1' int(11) DEFAULT NULL,
  'num2' int(11) DEFAULT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from numbers;
+----+------+------+
| id | num1 | num2 |
+----+------+------+
|  0 |    3 |    4 |
+----+------+------+
1 row in set (0.00 sec)

mysql> select num1 * num2 from numbers where id = 0;
+-------------+
| num1 * num2 |
+-------------+
|          12 |
+-------------+
1 row in set (0.01 sec)

Post Status

Asked in February 2016
Viewed 3,364 times
Voted 10
Answered 2 times

Search




Leave an answer