0

I need to create a view for with resulting math function multiply. Is this possible?

Table:

mysql> show create table devices \G
Create Table: CREATE TABLE `devices` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `d_type` varchar(255) NOT NULL,
  `multiplier` char(255) NOT NULL DEFAULT '',
  `value` decimal(10,3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Sample rows:

mysql> SELECT devices.id, devices.d_type, devices.multiplier, devices.`value`  FROM devices;   
+----+--------------------------+------------+--------+
| id | d_type                   | multiplier | value  |
+----+--------------------------+------------+--------+
|  1 | Cisco Call Manager       | /          |  4.000 |
|  2 | Generic Router/Switch    | *          |  0.350 |

I need to somehow calculate using the multiplier to provide a result for various math. Example (not working of course):

SELECT devices.id, devices.d_type, devices.multiplier, devices.`value`, (SELECT 1 $multiplier devices.`value`) as EPS FROM devices; 

The result in this case should be:

+----+--------------------------+------------+--------+--------+
| id | d_type                   | multiplier | value  |   EPS  |
+----+--------------------------+------------+--------+--------+
|  1 | Cisco Call Manager       | /          |  4.000 |  0.25  |
|  2 | Generic Router/Switch    | *          |  0.350 |  0.35  |

we need to derive the EPS column by dividing or multiplying 1 by the value column. Is there a way to use that multiplier as a determination of whether to divide or multiply?

(note that I used 1 as an example, this could be any incoming integer from user input)

2 Answers 2

1

You can use this query

SELECT devices.id, devices.d_type, devices.multiplier, devices.`value`, IF(multiplier = '/', 1/value, 1*value) as EPS FROM devices;

it works for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!For the sake of completeness in case someone needs this later on, there was a syntax error. Here's the correct command: SELECT devices.id, devices.d_type, devices.multiplier, devices.value, (IF(multiplier = '/', 1/value, 1*value)) AS EPS FROM devices;
0
IF(multiplier = '/', 1/value, value) AS EPS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.