user5145953
user5145953

Reputation:

MySql SELECT with if statement and LIKE operator

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.

Upvotes: 5

Views: 18157

Answers (1)

Mihai
Mihai

Reputation: 26784

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%'

Upvotes: 8

Related Questions