0

In a table in my database, I have a column that stores status values for posts. So if a post is published, it's 0, draft = 1, if it's yet to be looked at by a moderator - it's 2, if it's deleted - it's 3

So table looks like:

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

I need to display an icon for each post based on it's status. Can I do something like this in a sql query itself instead of writing more php code:

Eg:

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

I hope you got the idea. (The above sql statement is just for example) Can something like this be done.

1 Answer 1

2

You can use the CASE expression, like so:

select 
  postid as pId, 
  CASE 
    WHEN status = 0 THEN '<img src="published.png"'
    ELSE '<img src="draft.png"' 
  END AS status
FROM table
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Did the job. Thank you very much for this :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.