sql - how to approach this specific query -
i have rather complex logical query i'm trying execute. there series of related tables. having difficulty figuring out way approach it.
tables:
transaction -
t_id
discount -
d_id
item_id
type (percentage or basic value)
value
transaction_discount -
t_id
d_id
item -
item_id
price
edit: additional table
purchase
t_id
item_id
i attempting pull price adjusted after discount applied. avoid adding column adjusted price database... possible calculate adjusted price each transaction data entered (so it'd redundant store data).
this basic logic:
if(this transaction had discount applied) //apply discount , return adjusted price else(no discount applied) //pull price
it possible in several separate queries using php logic.
//1st step - create array of bool's: true = discount_used/false = no_discount //2nd step - if(discount_used) return price adjusted discount //3rd step - if(no_discount) return price //4th step - combine 2 different arrays
this bulky. seems there has better way more efficient.
it understanding can perform queries contain logic in mysql, here?
lets have item_id on transactions table, , percentage represented 'p', try like
select *, case when td.t_id null it.price when td.t_id not null , d.type = 'p' id.price - (id.price * d.value) else id.price - d.value end pricediscounted transaction t left join purchase p on t.t_id = p.t_id left join item on p.item_id = it.item_id left join transaction_discount td on t.t_id = td.t_id left join discount d on td.d_id = d.d_id left join item id on d.item_id = id.item_id
Comments
Post a Comment