如何在MySQL查询中将布尔值转换为整数。我尝试了无数次,事情变得空无一物。我只是想将true / false的“值”列分别转换为1和0到“最终值”列中。请看截图的链接。
MySQL查询:
SELECT
UNIX_TIMESTAMP(timestamp) as time_sec,
topic as metric,
data as value,
cast(DATA AS SIGNED) as finalvalue
FROM tcl_mqtt_log
WHERE topic = 'jace/home/heatpump/uvlight_ss';
答案 0 :(得分:2)
我认为您需要在此处使用CASE
表达式:
SELECT
UNIX_TIMESTAMP(timestamp) AS time_sec,
topic AS metric,
data AS value,
CASE WHEN data = 'true' THEN 1 ELSE 0 END AS finalvalue
FROM tcl_mqtt_log
WHERE topic = 'jace/home/heatpump/uvlight_ss';