Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL conditional AND clause?

In my query I want to add the and clause in query if a specific condition occur. See the where clause of my query

1     and ps.dept=dept.deptid  

2     and ps.sdept=deptsub.deptid 

3     and

4   if(ps.dept<>ps.sdept) 

5    begin

6    deptsub.Parent=dept.deptid 

7  end

8  and ps.deptcategory=deptcat.category

At 4 I want if condition fulfil then 6 should be added in query else not how is this possible.thanks!

like image 565
Zain Ali Avatar asked Aug 25 '26 20:08

Zain Ali


1 Answers

How about:

and ps.dept=dept.deptid  
and ps.sdept=deptsub.deptid 
and ( ps.dept=ps.sdept or deptsub.Parent=dept.deptid )
and ps.deptcategory=deptcat.category
like image 179
ysth Avatar answered Aug 28 '26 15:08

ysth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!