Trouble in MySQL querying -
i developing digital mobile loyalty app , have face problem in querying mysql code...
i have 4 tables: customer, card, redemption, merchant
currently trying want system first check if customer possess loyalty card of merchant when making redemption
- if yes, can proceed
- if no, system create card them , proceed
the redemption code generated merchant...
the problem how should query code so?
customer - customer_id - c_email - c_password - c_name card - card_id - chop_amt - customer_id* - merchant_id* merchant - merchant_id - merchant_name redemption - red_id - red_code - card_id* - merchant_id*
i tried writing code own now...can some1 please me check?
select * customer customer join card card on customer.customer_id = card.customer_id join redemption redemption on card.merchant_id = redemption.merchant_id card.merchant_id = redemption.merchant_id , redemption.red_code = 'a002'
i recommend putting unique index on card table merchant / customer entries. if go approach, can insert ... on duplicate key update id = id. ensure record exists regardless.
to create index, this
create unique index customer_merchant on card (customer_id, merchant_id);
now have in place, go ahead , insert, not insert new row if 1 exists
insert card (chop_amt, customer_id, merchant_id) values(0.00, 1, 1) on duplicate key update card_id = card_id
the update card_id = card_id part alias "don't anything", it's setting card_id itself
Comments
Post a Comment