Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating IF statement that if "Variable" != NULL then WHERE clause

Tags:

sql

mysql

I want to have a query that will let me select ALL from the table if the variable is null, and my only option here is PURE MYSQL here is my CODE

SELECT *
FROM tblPersonalData
if(VARIABLE!=null,WHERE    Studno=VARIABLE},'all')
like image 254
Aff Aefd Avatar asked Mar 09 '26 23:03

Aff Aefd


2 Answers

It's simple OR operation:

select
*
from tblPersonalData
where variable is null
or studNo = variable;
like image 167
Gurwinder Singh Avatar answered Mar 11 '26 13:03

Gurwinder Singh


You can emulate this behavior with the logical or operator. Note, however, that null is not a value, and you should test it explicitly with the is or is not operators, not = or !=:

SELECT *
FROM   tblPersonalData
WHERE  variable IS NULL OR sutdno = variable
like image 20
Mureinik Avatar answered Mar 11 '26 12:03

Mureinik



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!