Given this data, I want to sum (tbl_basket subt), multiply by discount, add shipping charge.
# tbl_basket order_id qty price subt
1 1 10.00 10.00
1 4 5.00 20.00
1 3 12.00 36.00
# tbl_coupon id name discount
1 "10% off" 10.00
# tbl_shipping id cost
1 4.99
# tbl_order coupon_id shipping_id total
1 1 ? # I want to update total
I want to query the basket:
SELECT * FROM tbl_basket WHERE order_id = $order_id
Sum the items, i think like this?
SELECT SUM(subt) FROM tbl_basket WHERE order_id = $order_id
Query the discount
SELECT discount FROM tbl_coupon AS coupon
INNER JOIN tbl_order AS order
WHERE coupon.id = order.coupon_id
Q) How do I merge this query into the other one in MySQL?
Q) How do set the value after the discount (new total = total*(100-discount)/100)
I felt it would make the system more robust by summing totals from the database directly, thats why I wanted to do it this way.
Q) Is there a better way to do this?
Thanks for your help.
I agree with evan – doing the calculations in PHP is much better. If nothing else, you can better see what you’re doing!
But if you really want to see what it looks like in SQL, you can try something like this:
Sure, with subqueries you might get everything into a single query statement. The more you add, though, the harder it is to debug. In PHP it’s much easier to assemble the pieces.
Good luck.