D12L12
D12L12

Reputation: 25

If Statement in MySQL?

Here is the problem:

I have multiple membership levels and per account the user gets displayed a different price amount for the product. Although, throughout the whole project its all SQL Query's for the price and it's too much to change.

Is there a way where I can call the current user id in the MYSQL database that the person is logged into and lets say they Gold Member they get price * .50 = (their new product price) silver member: price * .75

Now based on their account, the price gets multiplied and product_price will now return that new amount instead of the default $100 lets say.

Thank you.

Upvotes: 1

Views: 102

Answers (2)

Barmar
Barmar

Reputation: 780984

Use a CASE expression:

SELECT CASE member_level
        WHEN 'gold' THEN price * 0.50
        WHEN 'silver' THEN price * 0.75
        ELSE price
       END AS discounted_price
FROM ...

Upvotes: 3

ntadic
ntadic

Reputation: 21

If you have member level in that query you can use case:

select case when ?member_level='gold' then price * .50 else price end as final_price from products

Upvotes: 0

Related Questions