# MySQL Syntax Help

**URL:** <https://forum.inductiveautomation.com/t/mysql-syntax-help/8749>\
**Category:** Ignition\
**Created:** [July 17, 2014, 10:26pm UTC](https://forum.inductiveautomation.com/t/mysql-syntax-help/8749 "2014-07-17T22:26:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreyaslimaye](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@shreyaslimaye](https://forum.inductiveautomation.com/u/shreyaslimaye)\
**Post date:** [July 17, 2014, 10:26pm UTC](https://forum.inductiveautomation.com/t/mysql-syntax-help/8749/1 "2014-07-17T22:26:40Z")

</div>

Hello,

Can someone guide me on using If Else statement in where clause in MySQL query?

This is what I want to do:

distinct(op.Name), ro.PredictedValue, ro.TargetValue, sops.ActualValue  
FROM outputcomponents oc  
JOIN runoutputs ro ON ro.SubComponentID = oc.ID  
JOIN outputs op ON op.ID = oc.OutputID  
JOIN sendoutputs sops ON ro.SubComponentID = sops.SubComponentID  
WHERE (  
IF (SELECT count(SendDataID) from sendoutputs where RunID = 257) \> 0)  
THEN (ro.RunID = 257 and sops.SendDataID in (SELECT max(SendDataID) from sendoutputs where RunID = 257))  
ELSE (ro.RunID = 257)  
)  
Order by oc.OutputID

But I get a MySQL syntax error when I try that. What is the correct syntax to add If Then Else condition in Where statement?

Thanks,

Shreyas

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 18, 2014, 11:47am UTC](https://forum.inductiveautomation.com/t/mysql-syntax-help/8749/2 "2014-07-18T11:47:43Z")

</div>

Hi Shreyas,

IF functions in MySQL are like Excel:

IF(expression\_to\_test, expression\_if\_true, expression\_if\_false)

So yours would be something like:

`IF ((SELECT count(SendDataID) from sendoutputs where RunID = 257) > 0),
    (ro.RunID = 257 and sops.SendDataID in (SELECT max(SendDataID) from sendoutputs where RunID = 257)),
    (ro.RunID = 257))`

Don’t know if using an IF in a WHERE clause is the best way to go or not, but sometimes it can’t be helped. 🙂
