2012-12-18 61 views
0

在我的數據庫的一個表中,我有一個存儲帖子狀態值的列。因此,如果一個帖子的發佈,它是0,草案= 1,如果是尚未在由主持人來看待 - 這是2,如果是刪除 - 這是3在sql語句中做一個if elseif

所以表如下所示:

postid | status 
---------------- 
    1  0 
    2  1 
    3  2 

我需要根據每個帖子的狀態顯示一個圖標。我可以做這樣的事情在一個SQL查詢本身,而不是寫更多的PHP代碼:

如:

select postid as pId, status as status (if status = 0 {status = '<img src="published.png"'} elseif if status =1 {status = '<img src="draft.png"'} 

我希望你有這個想法。 (上面的sql語句只是舉例) 可以這樣做。

回答

2

可以使用CASE表達,就像這樣:

select 
    postid as pId, 
    CASE 
    WHEN status = 0 THEN '<img src="published.png"' 
    ELSE '<img src="draft.png"' 
    END AS status 
FROM table 
+0

哇!做了這份工作。非常感謝你:-) – Norman