I have a subquery in SELECT and sometimes it returns more than one row. I want to solve the problem like this:
Query:
select
s.id,
(select description from comment c where c.id = s.id)
from student s;
thanks.
You can join, aggregate, and use a conditional expression:
select
s.id,
case when min(c.description) <> max(c.description)
then 'multi'
else min(c.description)
end description
from student s
left join comment c on c.id = s.id
group by s.id
You can also do this with a subquery, which avoids outer aggregation (it is handy if you need more columns from students
):
select
s.*,
(
select
case when min(c.description) <> max(c.description)
then 'multi'
else min(c.description)
end
from comment c
where c.id = s.id
) description
from student s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.