1

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.

3
  • 2
    Use the IF() function or a CASE expression. Commented Jan 19, 2017 at 0:06
  • I know that, but I mean where would I put it. Im using Sequel Pro Commented Jan 19, 2017 at 0:09
  • Sequel Pro is for running SQL queries by hand, it has nothing to do with running queries from a PHP script. Commented Jan 19, 2017 at 0:29

2 Answers 2

3

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 ... 
Sign up to request clarification or add additional context in comments.

7 Comments

Barmar strikes again. Excellent answer!
How can I get the current user id. I have a query I use $this->session->user_id
Bind it as a parameter to the prepared statement, just like getting any other variable into a query.
In the mySQL or in the actual project?
or Is there an option to automatically get the session while writing this SELECT CASE in the MySQL?
|
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.