I have to write a query where the where
clause should work on only if the field value is not empty else all record should be fetched.
I have a table name post
id | valid_month | name |
------+-------------+------|
1 | 1,2,3 | post1|
2 | 1,2 | post2|
3 | | post3|
4 | 2,5 | post4|
5 | | post5|
----+-------------+------+
I want query like
select * from post where IF(valid_month != "") THEN valid_month REGEXP '[[:<:]]3[[:>:]]' END IF;
and result should like
id | valid_month | name |
------+-------------+------|
1 | 1,2,3 | post1|
3 | | post3|
5 | | post5|
----+-------------+------+
Is there any way to get this output in MySQL?