简体   繁体   中英

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.

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;

You could use something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM