tags:

views:

44

answers:

2
+1  Q: 

mysql if condition

I have to create a query and in that query I need to be able to do this

select if(filename is like .jpg/gif) as type from pictures;

I basically need to send the type of the picture through but they can be either jpg or gif but I don't know the syntax for the if like condition.

tried this and still nothing

select fileType, IF(fileType LIKE '%.jpg' OR fileType LIKE '%.gif', 'jpg', 'gif')from Pictures ......

   +-------------------------+--------------------------------------------------------------------+
| Picture                   | type  |
+-------------------------+----------------------------------------------------
| 1554588136_vibsbujy.jpg |   jpg                                                                |
| 1554626891_powioojt.gif |   jpg                                                                |

same result both times

+1  A: 
Select Case 
        When FileName Like '.jpg%' Or FileName Like '.gif%' Then FileName
        End
From Pictures

It is not clear what you want returned should the condition be met nor what should be returned if the condition is not met. If all you want to do is filter for files with these extensions then you can do something like:

Select FileName
From Pictures
Where FileName Like '.jpg%' 
    Or FileName Like '.gif%'
Thomas
A: 

If you are still interested in the IF syntax, this is how you should use it.

SELECT fileType,
       IF(fileType LIKE '%.jpg',
          'jpg',
          IF(filetype LIKE '%.gif',
             'gif',
             'unknown type')) AS type
  FROM Pictures
mhitza