开发者

mysql if condition

开发者 https://www.devze.com 2023-01-12 22:53 出处:网络
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 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 f开发者_开发问答ileType, 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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消