假设有一张表记录了“结算价purchase,折扣discount, 成本cost”,另一张表记录了“记账公式formula”,能否通过查询语句(最好)或存储过程,计算出各个订单的记账金额?
-- 初始化语句如下:
-- 建表
DROP TABLE IF EXISTS order
;
CREATE TABLE order
(id
int(10) NOT NULL AUTO_INCREMENT,purchase
decimal(10,2) DEFAULT '0.00' COMMENT '结算价',cost
decimal(10,2) DEFAULT '0.00' COMMENT '成本价',discount
decimal(10,2) DEFAULT '0.00' COMMENT '折扣',
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS order_formula
;
CREATE TABLE order_formula
(id
int(10) NOT NULL AUTO_INCREMENT,order_id
int(10) COMMENT '订单ID',formula
VARCHAR(50) COMMENT '计算公式',
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 插入数据
INSERT INTO test
.order
(id
, purchase
, cost
, discount
) VALUES (1, 150.00, 200.00, 0.80) ,(2, 180.00, 200.00, 0.80);
INSERT INTO test
.order_formula
(order_id
, formula
) VALUES ( 1, 'least(costdiscount,purchase)'),( 2, 'costdiscount+10');
-- 表连接
select a.*,b.formula from order
a,order_formula
b WHERE a.id=b.order_id

MYSQL能否实现将字符串转换为表达式进行计算?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-