为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】: DM8
【操作系统】:linux
【CPU】: 16核
【问题描述】*:
我的sql中用一个if没有问题:
set schema mycommunity_szjgxctest;
SELECT
COUNT(*)
FROM
(
SELECT
"proj"."name" AS "proj_name",
IF
((
SELECT "online_status" FROM "q_aiot_norm_device_online_status" AS "o"
WHERE "o"."device_id" = "iot"."id"
ORDER BY "occurrence_time" DESC LIMIT 1
) IS NULL,
0,(
SELECT "online_status" FROM "q_aiot_norm_device_online_status" AS "o" WHERE
"o"."device_id" = "iot"."id"
ORDER BY
"occurrence_time" DESC
LIMIT 1
)) AS "status",
"iot"."type_code" AS "device_code",
"iot"."created_on" AS "created_on"
FROM
"q_aiot_norm_device" "iot"
LEFT JOIN "t_project" "proj" ON "iot"."proj_id" = "proj"."id"
WHERE
( "iot"."is_deleted" = 1 )) "d"
而包含了两个if就会报无法解析的成员访问表达式 IF
set schema mycommunity_szjgxctest;
SELECT
COUNT(*)
FROM
(
SELECT
"proj"."name" AS "proj_name",
IF
((
SELECT "online_status" FROM "q_aiot_norm_device_online_status" AS "o"
WHERE "o"."device_id" = "iot"."id"
ORDER BY "occurrence_time" DESC LIMIT 1
) IS NULL,
0,(
SELECT "online_status" FROM "q_aiot_norm_device_online_status" AS "o" WHERE
"o"."device_id" = "iot"."id"
ORDER BY
"occurrence_time" DESC
LIMIT 1
)) AS "status",
IF
((
SELECT "occurrence_time" FROM "q_aiot_norm_device_online_status" AS "o" WHERE "o"."device_id" = "iot"."id"
ORDER BY "occurrence_time" DESC LIMIT 1
) IS NULL,
0,(
SELECT "occurrence_time" FROM "q_aiot_norm_device_online_status" AS "o"
WHERE
"o"."device_id" = "iot"."id"
ORDER BY
"occurrence_time" DESC
LIMIT 1
)) AS "status_time",
"iot"."type_code" AS "device_code",
"iot"."created_on" AS "created_on"
FROM
"q_aiot_norm_device" "iot"
LEFT JOIN "t_project" "proj" ON "iot"."proj_id" = "proj"."id"
WHERE
( "iot"."is_deleted" = 1 )) "d"
解决了 ,if 要改成 case when xx then xx else xx end可以
