0

我需要为生成的数学函数乘法创建一个视图。这可能吗?

桌子:

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 除以或乘以 value 列来得出 EPS 列。有没有办法使用该乘数来确定是除还是乘?

(请注意,我以 1 为例,这可以是来自用户输入的任何传入整数)

4

2 回答 2

1

您可以使用此查询

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

它适合你。

于 2012-08-16T04:29:36.833 回答
0
IF(multiplier = '/', 1/value, value) AS EPS
于 2012-08-16T04:25:58.660 回答