MySQL,如何在查询中将True / False转换为1/0

时间:2018-07-30 05:42:50

标签: mysql int boolean

如何在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';

screenshot of mysql_query_result

1 个答案:

答案 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';