您好我有以下脚本,当我运行它时,会根据客户特定的设定价格返回与商品相关的正确信息和正确的定价。
这一切都可行,但是当我想要总计所有项目并按订单ID分组时,我遇到了麻烦。我确实在truncate前面尝试了一个sum函数,但这返回了不正确的价格。
干杯
SELECT
w.ward_name,
orders.id as orderID,
if ((SELECT count(price) from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id)>0,(SELECT price from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id),(SELECT price from products where op.product_id = products.id) )as price,
TRUNCATE(op.product_quantity * (SELECT(price)),2) as 'total price',
from_unixtime(orders.date_filled, '%Y-%m-%d') as 'filled date' FROM `orders` join wards w ON orders.ward_id = w.id
join order_products op on orders.id = op.order_id
and op.product_quantity > 0
AND from_unixtime(date_filled,'%Y-%m-%d') >= '2017-04-01'
and from_unixtime(date_filled,'%Y-%m-%d') <= '2017-04-30'
and ward_category = 138
and orders.id = 13522
样本数据
这就是我现在看到的
ward_name orderID price total price filled date
Customer 13522 0.10 20.00 2017-04-06
Customer 13522 0.10 20.00 2017-04-06
Customer 13522 0.10 20.00 2017-04-06
Customer 13522 0.10 20.00 2017-04-06
我想看到什么
ward_name orderID price total price filled date
Customer 13522 0.10 80.00 2017-04-06
希望这有帮助
我试过这个
SELECT
w.ward_name,
orders.id as orderID,
if ((SELECT count(price) from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id)>0,(SELECT price from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id),(SELECT price from products where op.product_id = products.id) )as price,
TRUNCATE(op.product_quantity * (SELECT(price)),2) as 'total price',
from_unixtime(orders.date_filled, '%Y-%m-%d') as 'filled date' FROM `orders` join wards w ON orders.ward_id = w.id
join order_products op on orders.id = op.order_id
and op.product_quantity > 0
AND from_unixtime(date_filled,'%Y-%m-%d') >= '2017-04-01'
and from_unixtime(date_filled,'%Y-%m-%d') <= '2017-04-30'
and ward_category = 138
and orders.id = 13522
GROUP BY orderid
但它没有返回正确的值。我也尝试使用sum函数,这确实返回了不同的结果,但它也不正确
SELECT
w.ward_name,
orders.id as orderID,
if ((SELECT count(price) from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id)>0,(SELECT price from price_file where op.product_id = price_file.item_id and w.pf_id = price_file.pf_id),(SELECT price from products where op.product_id = products.id) )as price,
sum(TRUNCATE(op.product_quantity * (SELECT(price)),2)) as 'total price',
from_unixtime(orders.date_filled, '%Y-%m-%d') as 'filled date' FROM `orders` join wards w ON orders.ward_id = w.id
join order_products op on orders.id = op.order_id
and op.product_quantity > 0
AND from_unixtime(date_filled,'%Y-%m-%d') >= '2017-04-01'
and from_unixtime(date_filled,'%Y-%m-%d') <= '2017-04-30'
and ward_category = 138
and orders.id = 13522