5

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.

4
  • 1
    What`s the expected result? Commented Jul 31, 2015 at 9:58
  • To show only the columns which has 'default' in them Commented Jul 31, 2015 at 9:59
  • If you can provide table structure and expected result it would be great Commented Jul 31, 2015 at 10:00
  • 1
    Can you share your table's structure, some sample data and the result you're trying to achieve please? Commented Jul 31, 2015 at 10:00

1 Answer 1

8

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%'
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Mihai. Thanks. I am almost there. The Field contains a pathway to an image saved in a directory on the server. Your SELECT works and gives me a result, but instead of showing me the pathway, it shows 'Monday'
@Anoniem I edited it assuming path is the column,replace path with your column name where the path is stored
If it doesn't contain the 'default' it shows the column name with no path. Which is fine. Is there a way to exclude the column from the result if 'default' is not in the column content?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.