有人知道为什么CASE WHEN()总是返回“是”,即使有一些数据记录(按RecordID)在data_records_ga表中没有条目吗?
目前我不确定为什么最外面的SELECT * FROM (...) AS X
在那里,但是我的问题在于内部SELECT data_records.RecordID, (...
及其相关的子查询。
对于不满足子查询条件的记录,我希望看到“否”,并将其插入“已完成”字段。
SELECT * FROM (
SELECT
data_records.RecordID,
(
CASE WHEN (
SELECT COUNT(drga.Comment) AS count
FROM `data_records_ga` drga
INNER JOIN `data_records` dr
ON dr.RecordID = drga.RecordID
WHERE drga.FieldName = 'GA_completed'
AND drga.Comment = 1
AND dr.RecordID = drga.RecordID
HAVING COUNT(drga.Comment) > 0
) > 0
THEN 'Yes'
ELSE 'No'
END
) AS Completed,
FROM `data_records`
LEFT JOIN data_records_ga on data_records.RecordID = data_records_ga.RecordID
GROUP BY RecordID ORDER BY RecordID
) AS X
答案 0 :(得分:1)
这很可能是因为您的子查询未以任何方式链接到其父查询,因此您只选择了将获得“是”的行。尝试将此行添加到子查询的WHERE子句中:
AND dr.RecordID = data_records.RecordID
并删除:
AND dr.RecordID = drga.RecordID
上一行是多余的,因为您已经在子查询中将表连接在一起了。