2

My MySQL table has these columns: Status : int(11) and Closed : int(1). The thing is that when I retrieve rows with SELECT, I want Closed to be treated as a Status value.

So, whenever Closed = 1, I want Status to be equal to -1. It's important to say that I need Status to be stored independently of the Closed column. Is that possible to do with the SELECT query or do I have to make like a PHP workaround for it ?

2
  • why can't you update the value of Status when Closed getting updated ? Commented Sep 30, 2017 at 12:22
  • Because, when the Closed value is updated back to 0, I need the previous Status value. That's why Status cannot be changed. Commented Sep 30, 2017 at 12:24

1 Answer 1

7

You could use CASE statement

SELECT Closed,
CASE
    WHEN Closed = 1 THEN 
    -1
    ELSE Status
END AS Status
FROM <YOUR_TABLE>; 
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I wanted. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.