I have a table as follows:
product | quantity | price | gift | giftprice
--------|----------|-------|------|----------
1 | 2 | 9.99 | 0 | 4.99
2 | 3 | 3.50 | 1 | 2.25
3 | 1 | 4.75 | 1 | 1.50
What I’d like to have an SQL query that will give me a figure that gives me the sum of all the records with quantity multiplied by price with the giftprice being added to the price before multiplication only if the ‘gift’ field is set to 1.
Pseudocode
foreach(record){
if(gift == 1){ linetotal = (price + giftprice) * quantity; }
else { linetotal = price * quantity; }
total = total + linetotal;
}
You can just do:
Since nothing will be added if
gift = 0.