Skip to content
Advertisement

How to modify result of select statement based on values?

I am not MySQL guy just learning by doing things, I have database that stores value 0 or 1, now in grafana I want to display online if value yield from select statement is 1 and offline if value of online in select statement is 0.

So far this is what I wrote

SELECT  "online" if online == 1 else "offline", datetime_added as time from roku_online_status order by id DESC LIMIT 1;

But select statement through error.

Advertisement

Answer

You’re looking for a case expression:

SELECT   CASE online WHEN 1 THEN 'online' ELSE 'offline' END, datetime_added AS time
FROM     roku_online_status
ORDER BY ID DESC
LIMIT    1;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement