0

我可以在 Where 里面做一个 if 吗?或仅当字段不为空时才允许我进行检查的东西(路径=空)

SELECT  
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto  
WHERE profilo_id = 15 
 -- only if path != "/uploads/attachments/default/thumbnails/avatar.png"
AND foto_eliminata = 0 AND foto_profilo = 1
4

3 回答 3

1

你可以试试这个

SELECT  
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto  
WHERE profilo_id = 15 
AND IF( path != "/uploads/attachments/default/thumbnails/avatar.png", 
         foto_eliminata = 0 AND foto_profilo = 1,
        foto_eliminata like '%' AND foto_profilo like '%'
        )
于 2013-10-24T09:55:32.490 回答
0

您可以为此使用二进制逻辑:

SELECT * FROM TABLE WHERE A
(if b then also where C)

变成

SELECT * FROM TABLE WHERE A 
AND (!b OR C)

在你的情况下:

SELECT  
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto  
WHERE profilo_id = 15 
AND (path = "/uploads/attachments/default/thumbnails/avatar.png" OR foto_eliminata = 0 AND foto_profilo = 1))
于 2013-10-24T09:52:06.627 回答
0

是的,您可以IF()在期望值的任何地方使用,即使大多数时候它不是最佳途径。

WHERE IF(path IS NULL, portfolio_id = 15, TRUE)

但更喜欢:

WHERE path IS NULL OR portfolio_id = 15

(请注意,您不能与NULL,[anything] = NULL总是比较FALSE

于 2013-10-24T09:52:41.797 回答