我有一个如下所示的查询结构,我想知道是否有一种方法可以使用 CASE 语句或通过其他方式将选择查询编写为一个查询,以便根据它们的值将值插入到适当的变量中。
DECLARE passes INT;
DECLARE fails INT;
..
SELECT count(score)
INTO passes
FROM scores
WHERE score >= 40;
SELECT count(score)
INTO fails
FROM scores
WHERE score < 40;
默多克想出了一个巧妙的解决方案来解决这个问题,我只需要对其进行一次更改即可将每个值放入相应的变量中
SELECT *
INTO passes, fails
FROM (SELECT SUM(CASE
WHEN score >= 40 THEN 1
ELSE 0
END) AS _passes,
SUM(CASE
WHEN score < 40 THEN 1
ELSE 0
END) AS _fails
FROM scores) AS x;