我从下面的查询中获取逗号分隔值
SELECT LISTAGG('''' || student_name || '''',',') WITHIN GROUP (ORDER BY student_name) FROM students;
但是,当我放入子查询时,它不会给出任何结果。
select * from students where student_name in ('A', 'B'); --提供两行
select * from students where student_name in (SELECT LISTAGG('''' || student_name || '''',',') WITHIN GROUP (ORDER BY student_name) FROM students;) --没有行
发布于 2018-02-04 21:35:14
您不了解in是如何工作的。它不适用于字符串。它可以处理多个项目。
因此,编写逻辑的正确方法是:
select *
from students
where student_name in (select student_name from students) -- no rows;您的查询实际上是在执行以下操作:
where student_name in ('aaron,beth,calvin,debbie,...')in列表只有一项,所以这相当于:
where student_name = 'aaron,beth,calvin,debbie,...'并且没有学生姓名(如果您有多行)可以是所有学生的串联姓名。
https://stackoverflow.com/questions/48608592
复制相似问题