我想在MySQL数据库中获得某些服务的价格,计算方法如下:
我有4张桌子:
关系:
日期和服务之间的一对多:因此,服务可以在几个日期进行,每个日期都有相关的公里(以量化旅行费用) ,最终服务价格每个0.6欧元)和挂载(如果不活跃在每个日期,服务价格将递减由a恒定值,在我的情况下为100欧元)......
通过Features_Services 在功能和服务之间进行多对多:因此,许多服务可以关联许多功能。每个功能都有单一价格,但关系也有单位和折扣等参数......
嗯,通过以下查询,我可以正确获得服务的价格,但仅限于公里和第一个日期的安装 ......
SELECT
CASE
WHEN dates.mounting = 0 THEN
SUM(features.price * features_services.units * (1 - features_services.discount / 100)) + 0.6 * dates.kilometers - 100
ELSE
SUM(features.price * features_services.units * (1 - features_services.discount / 100)) + 0.6 * dates.kilometers
END AS servicePrice
FROM features_services
JOIN features ON features_services.idFeature = features.idFeature
JOIN dates ON features_services.idService = dates.idService
WHERE features_services.idService = XXXXXXX
GROUP BY dates.idDate LIMIT 1
问题是我需要遍历提取公里数和挂载的所有相关日期,并将必要的更正应用于价格...... 也许选择里面的选择,日期??
非常感谢任何帮助。 比你提前。
答案 0 :(得分:0)
我终于使用select子查询做到了。这是解决方案:
SELECT
features_services.idService AS cid,
SUM(features.price * features_services.units * (1 - features_services.discount / 100)) +
(SELECT 0.6 * SUM(dates.kilometers) FROM dates WHERE dates.idService = cid) +
(SELECT SUM(CASE when dates.mounting = 1 THEN 0 else -100 END) FROM dates WHERE dates.idService = cid) AS importeTotal
FROM features_services
JOIN features ON features_services.idFeature = features.idFeature
WHERE features_services.idService = XXXXXXXX
谢谢。