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.
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()