Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing an "if" type statement in a sql where clause

Tags:

sql

I'm trying to do some sort of "if" statement in my where clause. I realize that sql doesn't support this but I'm sure there must be some way to make this work with sql syntax. As shown in the area I have in bold I'm trying to find all items that begin with d and filter them out if their userfld2 also = container.

Is there a more reasonable way to do this than I am doing or am I way off the mark?

Thanks in advance.

Select a.ItemID
   , b.ConversionFactor VCaseAmt
   , sum(c.ConversionFactor + 1) SCaseAmt
   , a.status
   , a.UserFld2
From timItem a
inner join timItemUnitOfMeas b on a.ItemKey = b.ItemKey
   and b.TargetUnitMeasKey = 115
left join timItemUnitOfMeas c on a.ItemKey = c.ItemKey
   and c.TargetUnitMeasKey = 116
left join timItemUnitOfMeas d on a.ItemKey = d.ItemKey
   and d.TargetUnitMeasKey = 126
Where d.TargetUnitMeasKey is null
   and b.ConversionFactor != c.ConversionFactor + 1
   and a.Status = 1
   and **(filter a.itemid not like 'd%' when a.userfld2 = 'Container')**
Group by a.ItemID, b.TargetUnitMeasKey, b.ConversionFactor, C.TargetUnitMeasKey
   , c.ConversionFactor, a.status, a.UserFld2
Order by a.ItemID
like image 935
Aarmora Avatar asked Apr 16 '13 18:04

Aarmora


People also ask

Can IF statement be in WHERE clause SQL?

IF… ELSE clause is very handy and whenever you need to perform any conditional operation, you can achieve your results using it. But there are some limitations in IF… ELSE, and one of the limitations is that you cannot use it in WHERE clause. Let me demonstrate the limitations. Incorrect syntax near the keyword 'IF'.

Can you put an if statement in a query?

The IF() function that can be used in queries is primarily meant to be used in the SELECT portion of the query for selecting different data based on certain conditions, not so much to be used in the WHERE portion of the query: SELECT IF(JQ.

Which conditions can we use with WHERE clause?

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specific value from the table. You should use the WHERE clause to filter the records and fetching only the necessary records.


1 Answers

Use this:

Select a.ItemID, b.ConversionFactor VCaseAmt, sum(c.ConversionFactor + 1) SCaseAmt, a.status, a.UserFld2

From timItem a inner join
    timItemUnitOfMeas b on a.ItemKey = b.ItemKey and b.TargetUnitMeasKey = 115 left join
    timItemUnitOfMeas c on a.ItemKey = c.ItemKey and c.TargetUnitMeasKey = 116 left join
    timItemUnitOfMeas d on a.ItemKey = d.ItemKey and d.TargetUnitMeasKey = 126

Where d.TargetUnitMeasKey is null and b.ConversionFactor != c.ConversionFactor + 1 and a.Status = 1 and 
not (a.itemid like 'd%' AND a.userfld2 = 'Container') 

Group by a.ItemID, b.TargetUnitMeasKey, b.ConversionFactor, C.TargetUnitMeasKey, c.ConversionFactor, a.status, a.UserFld2

Order by a.ItemID
like image 131
Lawson Avatar answered Oct 10 '22 05:10

Lawson