我需要为结果数学函数乘法创建一个视图。这可能吗?
表:
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)
示例行:
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 |
我需要以某种方式使用乘数来计算,以提供各种数学的结果。 示例(当然不工作):
SELECT devices.id, devices.d_type, devices.multiplier, devices.`value`, (SELECT 1 $multiplier devices.`value`) as EPS FROM devices;
这种情况下的结果应该是:
+----+--------------------------+------------+--------+--------+
| id | d_type | multiplier | value | EPS |
+----+--------------------------+------------+--------+--------+
| 1 | Cisco Call Manager | / | 4.000 | 0.25 |
| 2 | Generic Router/Switch | * | 0.350 | 0.35 |
我们需要通过将1乘以值列来得到EPS列。 有没有办法使用该乘数来确定是分频还是乘法?
(请注意,我使用1作为示例,这可以是来自用户输入的任何传入整数)
答案 0 :(得分:1)
您可以使用此查询
SELECT devices.id, devices.d_type, devices.multiplier, devices.`value`, IF(multiplier = '/', 1/value, 1*value) as EPS FROM devices;
它适合你。
答案 1 :(得分:0)
IF(multiplier = '/', 1/value, value) AS EPS