0

I want to use IF statement in SELECT Query. Here is the sample for that. I have 3 table which keeps customer info, and models info, and stocks. I want to offer best fit model to each individual customer. So in my first try if customer income less then 5000 i want to offer cheap models that exist in that store, else any model in that store. But i couldnt use IF clouse in SELECT WHERE section.

SELECT  TOP 1 @vmodel  = stocks.model , @vsloc = stocks.sloc , @vyear = models.year , @vcost = models.cost <br>FROM bistore.dbo.stocks 
JOIN bistore.dbo.models
ON stocks.model = models.model 
WHERE stocks.sloc<>'Main'
AND stocks.stock > 0 
AND models.mat IN (
IF (@vincome<5000) SELECT DISTINCT mat from models where mat<>'Carbon' 
ELSE SELECT DISTINCT mat from models 
)   /* If income is less than 5000 than chose cheap models , else whatever ) */

** UPDATE **
When a customer login to my portal i want to offer him/her best model fits to him. I will check his income, store stock close to him and offer him in banner. Here is my samples from tables.

CustomerInfo

ModelInfo

DECLARE @vcname VARCHAR(50)
DECLARE @vckids INT
DECLARE @vcleng INT
DECLARE @vcweig INT
DECLARE @vcincome INT
DECLARE @vyear INT
DECLARE @vcost INT

SELECT TOP 1 @vcname = nmsnm , @vckids = kids , @vcleng = leng, @vcweig = weig , @vcincome = income FROM customers ORDER BY id DESC


DECLARE @vmodel VARCHAR(50)
DECLARE @vsloc VARCHAR(50)
SELECT TOP 1 @vmodel = stocks.model , @vsloc = stocks.sloc , @vyear = models.year , @vcost = models.cost FROM bistore.dbo.stocks
JOIN bistore.dbo.models
ON stocks.model = models.model
WHERE stocks.sloc<>'Main'

AND stocks.stock > 0
AND models.mat IN (
IF (@vincome<5000) BEGIN SELECT DISTINCT mat from models where mat<>'Carbon' END
ELSE BEGIN SELECT DISTINCT mat from models END
) /* If income is less than 5000 than chose cheap models , else whatever ) /
AND models.frames IN (SELECT frames from models where frames<48) /
chose model fit to length /
AND models.type <> 'Kids Bike' /
if have kids , chose whatever (not bigger then length) */
ORDER BY NEWID()

1
  • can you give us some sample data and expected result Commented Sep 7, 2017 at 10:45

2 Answers 2

1

You don't need IF or CASE, just an additional AND and a reversed @vincome check:

SELECT TOP 1 
       @vmodel = stocks.model , 
       @vsloc =  stocks.sloc , 
       @vyear =  models.year , 
       @vcost = models.cost <br> 
FROM   bistore.dbo.stocks 
JOIN   bistore.dbo.models 
ON     stocks.model = models.model 
WHERE  stocks.sloc <>'Main' 
AND    stocks.stock > 0 
AND ( @vincome >= 5000 OR models.mat <> 'Carbon' )

You see that you don't even need a subquery.

Sign up to request clarification or add additional context in comments.

Comments

0

In MySQL it is the CASE statement of which details are here.

A possible usage (please refer your objects for correct syntax):

SELECT  TOP 1 @vmodel  = stocks.model , @vsloc = stocks.sloc , @vyear = models.year , @vcost = models.cost <br>FROM bistore.dbo.stocks 
JOIN bistore.dbo.models
ON stocks.model = models.model 
WHERE stocks.sloc<>'Main'
AND stocks.stock > 0 
AND models.mat IN (
    CASE sign(@vincome - 5000)
       WHEN -1 then (SELECT DISTINCT mat from models where mat<>'Carbon')
       ELSE '(SELECT DISTINCT mat from models ) 
    END
    )   /* If income is less than 5000 than chose cheap models , else whatever ) */

PS: Sign is a trick I use to mimic greater / equal / smaller control for better comprehension of CASE syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.