[英]MySQL error Subquery returns more than 1 row
I have the following query:我有以下查询:
SELECT
users.*,
classes.*,
evaluation.student_id,
evaluation.class_id,
evaluation.chapter_title,
(SELECT
`score`
FROM
`evaluation`
WHERE
`class_id` = 1
AND
`id`
IN
(SELECT
MAX(`id`)
FROM
`evaluation`
WHERE
`class_id` = 1
GROUP BY
`chapter_title`)
GROUP BY
`chapter_title`)
AS
`score`,
(SELECT
`total_score`
FROM
`evaluation`
WHERE
`class_id` = 1
AND
`id`
IN
(SELECT
MAX(`id`)
FROM
`evaluation`
WHERE
`class_id` = 1
GROUP BY
`chapter_title`)
GROUP BY
`chapter_title`)
AS
`total_score`
FROM
(`evaluation`
INNER JOIN
`users`
ON
evaluation.student_id=users.id)
INNER JOIN
`classes`
ON
evaluation.class_id=classes.id
WHERE
users.role='student'
AND
evaluation.class_id = 1
AND
evaluation.student_id = 8
But when I execute this query in phpmyadmin it displays a error message saying:但是当我在 phpmyadmin 中执行这个查询时,它会显示一条错误消息:
#1242 - Subquery returns more than 1 row
Whats wrong in the query.Please help.查询有什么问题。请帮忙。 Thanks in advance.提前致谢。
I have this tables:我有这个表:
users用户
classes班级
evaluation评估
In the evaluation table(last image).. I would only want to return a distinct chapter_title or a grouped chapter_title that has the highest id and has a student_id of 8.在评估表(最后一张图片)中。我只想返回一个不同的chapter_title 或一个分组的chapter_title,它具有最高的id 并且student_id 为8。
I needed to use this query... but returns an error.我需要使用这个查询...但返回一个错误。
The problem is that both of your queries return more than one record:问题是您的两个查询都返回多个记录:
SELECT `score`
FROM `evaluation`
WHERE `class_id` = 1
AND `id` IN (SELECT MAX(`id`)
FROM `evaluation`
WHERE `class_id` = 1
GROUP BY `chapter_title`)
GROUP BY `chapter_title`;
SELECT `total_score`
FROM `evaluation`
WHERE `class_id` = 1
AND `id` IN (SELECT MAX(`id`)
FROM `evaluation`
WHERE `class_id` = 1
GROUP BY `chapter_title`)
GROUP BY`chapter_title`;
See SQL Fiddle with demo请参阅带有演示的 SQL Fiddle
I altered your query slightly to the following:我将您的查询稍微更改为以下内容:
SELECT u.*,
c.*,
e.student_id,
e.class_id,
e.chapter_title,
(SELECT `score`
FROM `evaluation` e1
WHERE `class_id` = 1
AND e.id = e1.id
AND `id` IN (SELECT MAX(`id`)
FROM `evaluation`
WHERE `class_id` = 1
GROUP BY `chapter_title`)
GROUP BY `chapter_title`) AS`score`,
(SELECT `total_score`
FROM `evaluation` e1
WHERE `class_id` = 1
AND e.id = e1.id
AND `id` IN (SELECT MAX(`id`)
FROM `evaluation`
WHERE `class_id` = 1
GROUP BY `chapter_title`)
GROUP BY`chapter_title`) AS `total_score`
FROM `evaluation` e
INNER JOIN `users` u
ON e.student_id=u.id
INNER JOIN `classes` c
ON e.class_id=c.id
WHERE u.role='student'
AND e.class_id = 1
AND e.student_id = 8
You'll need to use LIMIT 1
on all of your subqueries, to ensure only one value is returned.您需要对所有子查询使用LIMIT 1
,以确保仅返回一个值。
Eg例如
select users.*, classes.*, evaluation.student_id, evaluation.class_id, evaluation.chapter_title, (
select `score`
from `evaluation`
where `class_id` = 1
and `id` in (
select MAX(`id`)
from `evaluation`
where `class_id` = 1
group by `chapter_title`
)
group by `chapter_title`
limit 1
) as `score`, (
select `total_score`
from `evaluation`
where `class_id` = 1
and `id` in (
select MAX(`id`)
from `evaluation`
where `class_id` = 1
group by `chapter_title`
)
group by `chapter_title`
limit 1
) as `total_score`
from (
`evaluation` inner join `users` on evaluation.student_id = users.id
)
inner join `classes` on evaluation.class_id = classes.id
where users.role = 'student'
and evaluation.class_id = 1
and evaluation.student_id = 8
Here is the corrected fiddle :这是更正的小提琴:
SELECT
users.*,
classes.*,
evaluation.student_id,
evaluation.class_id,
evaluation.chapter_title,
evaluation.score,
evaluation.total_score
FROM
evaluation
INNER JOIN users
ON
evaluation.student_id=users.id
INNER JOIN
classes
ON
evaluation.class_id=classes.id
WHERE
users.role='student'
AND
evaluation.class_id = 1
AND
evaluation.student_id = 8
AND
EXISTS
(SELECT
*
FROM
evaluation
WHERE
class_id = 1
HAVING MAX(id) = evaluation.id)
GROUP BY
chapter_title
Main changes:主要变化:
Instead of IN
, use EXISTS
(should be faster since it won't need to return all of it's sub-rows);而不是IN
,使用EXISTS
(应该更快,因为它不需要返回它的所有子行);
Some subqueries weren't needed.一些子查询是不需要的。
Most likely your SELECT scores
subquery (first one) is returning more than one row.很可能您的SELECT scores
子查询(第一个)返回不止一行。 It's the only one where it's not used in an IN()
clause, and its results are returned as a field in the parent query.它是唯一一个未在IN()
子句中使用的,其结果作为父查询中的一个字段返回。 Since it's "returning" to a field, it MUST result in a single value+single row result set.由于它“返回”到一个字段,它必须导致单个值+单行结果集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.