MySql SELECT with if statement and LIKE operator

12,694

you are using the if conditional branching which is only possible in procedures and triggers,use the if function.

SELECT if (`Monday` LIKE '%default%',`Monday`,''),
if (`Tuesday` LIKE '%default%',`Tuesday`,'')
FROM `Ad_Relationshp`


SELECT `Monday`,`Tuesday`
FROM `Ad_Relationshp`
WHERE `Monday` LIKE '%default%'
OR `Tuesday` LIKE '%default%'
Share:
12,694
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I am working on an advertisement site.

    I have one table with a column for each day. I need to check in the table which ads is saved as default to update only those ads.

    I have tried this, but it not working.

    SELECT Monday if (Monday LIKE '%default%'),
    Tuesday if (Tuesday LIKE '%default%')
    FROM `Ad_Relationshp`
    

    MySql gives me this error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if (Monday LIKE '%default%') FROM Ad_Relationshp LIMIT 0,' at line 1. I have tried changing the syntax to everything I can think off, but I still get the same error.