[英]error : #1242 - Subquery returns more than 1 row
I got an error: #1242 - Subquery returns more than 1 row when i run this sql. 我收到一个错误:#1242-当我运行此sql时,子查询返回的行多于1。
CREATE VIEW test
AS
SELECT cc_name,
COUNT() AS total,
(SELECT COUNT(*)
FROM bed
WHERE respatient_id > 0
GROUP BY cc_name) AS occupied_beds,
(SELECT COUNT(*)
FROM bed
WHERE respatient_id IS NULL
GROUP BY cc_name) AS free_beds
FROM bed
GROUP BY cc_name;
The problem is that your subselects are returning more than one value - IE: 问题是您的子选择返回多个值-IE:
SELECT ...
(SELECT COUNT(*)
FROM bed
WHERE respatient_id IS NULL
GROUP BY cc_name) AS free_beds,
...
...will return a row for each cc_name
, but SQL doesn't support compacting the resultset for the subselect - hence the error. ...将为每个
cc_name
返回一行,但是SQL不支持压缩子查询的结果集-因此出现错误。
Don't need the subselects, this can be done using a single pass over the table using: 不需要子选择,可以使用以下方法通过一次遍历表来完成:
SELECT b.cc_name,
COUNT(*) AS total,
SUM(CASE
WHEN b.respatient_id > 0 THEN 1
ELSE 0
END) AS occupied_beds,
SUM(CASE
WHEN b.respatient_id IS NULL THEN 1
ELSE 0
END) AS free_beds
FROM bed b
GROUP BY b.cc_name
This is because your subqueries (the SELECT
bits that are inside parentheses) are returning multiple rows for each outer row. 这是因为您的子查询(括号内的
SELECT
位)为每个外部行返回多个行。 The problem is with the GROUP BY
; 问题出在
GROUP BY
; if you want to use subqueries for this, then you need to correlate them to the outer query, by specifying that they refer to the same cc_name
as the outer query: 如果你想使用子查询这一点,那么你需要通过指定它们指向同它们关联到外部查询,
cc_name
作为外部查询:
CREATE VIEW test
AS
SELECT cc_name,
COUNT() AS total,
(SELECT COUNT()
FROM bed
WHERE cc_name = bed_outer.cc_name
AND respatient_id > 0) AS occupied_beds,
(SELECT COUNT(*)
FROM bed
WHERE cc_name = bed_outer.cc_name
WHERE respatient_id IS NULL) AS free_beds
FROM bed AS bed_outer
GROUP BY cc_name;
(See http://en.wikipedia.org/wiki/Correlated_subquery for information about correlated subqueries.) (有关相关子查询的信息,请参见http://en.wikipedia.org/wiki/Correlated_subquery 。)
But, as OMG Ponies and a1ex07 say, you don't actually need to use subqueries for this if you don't want to. 但是,正如OMG Ponies和a1ex07所说,如果您不想这样做,实际上不需要使用子查询。
Your subqueries return more than 1 row. 您的子查询返回超过1行。 I think you you need something like :
我认为您需要类似的东西:
SELECT COUNT(*) AS total,
COUNT(CASE WHEN respatient_id > 0 THEN 1 END) AS occupied_beds,
COUNT(CASE WHEN respatient_id IS NULL THEN 1 END) AS free_beds
FROM bed
GROUP BY cc_name
You can also try to use WITH ROLLUP
+ pivoting (mostly for learning purposes, it's a much longer query ) : 您还可以尝试使用
WITH ROLLUP
+旋转(主要是出于学习目的,这是一个更长的查询):
SELECT cc_name,
MAX(CASE
WHEN num_1 = 1 THEN tot_num END) AS free_beds,
MAX(CASE
WHEN num_1 = 2 THEN tot_num END) AS occupied_beds,
MAX(CASE
WHEN num_1 = IS NULL THEN tot_num END) AS total
FROM
(SELECT cc_name, CASE
WHEN respatient_id > 0 THEN 1
WHEN respatient_id IS NULL THEN 2
ELSE 3 END as num_1,
COUNT(*) as tot_num
FROM bed
WHERE
CASE
WHEN respatient_id > 0 THEN 1
WHEN respatient_id IS NULL THEN 2
ELSE 3 END != 3
GROUP BY cc_name,
num_1 WITH ROLLUP)A
GROUP BY cc_name
SELECT COUNT()
FROM bed
WHERE respatient_id > 0
GROUP BY cc_name
You need to remove the group-by in the sub query, so possibly something like 您需要在子查询中删除分组依据,因此可能类似于
SELECT COUNT(*)
FROM bed
WHERE respatient_id > 0
or possibly -- depending on what your application logic is.... 或可能-取决于您的应用程序逻辑是...
SELECT COUNT(*) from (
select count(*),cc_name FROM bed
WHERE respatient_id > 0
GROUP BY cc_name) filterview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.