mysql,如何在查询中将true/false转换为1/0

oxcyiej7  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(707)

这个问题在这里已经有答案了

mysql:返回“0”和“1”,而不是“false”和“true”(4个答案)
两年前关门了。
如何在mysql查询中将布尔值转换为整数。我试过无数次,结果都是空的。我只是希望将true/false的“value”列分别转换为1和0,并将其转换为“finalvalue”列。请看截图链接。
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';

mysql\查询\结果截图

dsf9zpds

dsf9zpds1#

我想你需要用一个 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';

相关问题