0

在我的数据库中的一个表中,我有一列存储帖子的状态值。因此,如果发布了帖子,则为 0,草稿 = 1,如果尚未由版主查看,则为 2,如果已删除,则为 3

所以表看起来像:

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语句只是一个例子)这样的事情可以做到吗。

4

1 回答 1

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
于 2012-12-18T04:29:23.547 回答