Michał Wawrzynowicz Michał Wawrzynowicz - 3 years ago 174
SQL Question

MySQL dynamic SELECT column value

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. To be more specific, 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?

Thanks for your help!

Answer Source

You could use CASE statement

SELECT Closed,
CASE
    WHEN Closed = 1 THEN 
    -1
    ELSE Status
END AS Status
FROM <YOUR_TABLE>; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download