从 WHERE 子句中的 SELECT 子句引用数组
refer to array from SELECT clause in WHERE clause
我有以下两个table:
create table person
(
identifier integer not null,
name text not null,
age integer not null,
primary key(identifier)
);
create table agenda
(
identifier integer not null,
name text not null,
primary key(identifier)
);
他们加入了以下 table:
create table person_agenda
(
person_identifier integer not null,
agenda_identifier integer not null,
primary key(person_identifier, agenda_identifier),
foreign key(person_identifier) references person(identifier),
foreign key(agenda_identifier) references agenda(identifier)
);
我试图在 WHERE
子句中引用 SELECT
子句中定义的数组。
以下作品:
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p;
这不是:
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p
where array_length(r, 1) >= 1;
它说 r
不是已知列。如何在 WHERE
子句中引用此数组?
我第二次查询的目的是:
- 忽略没有议程的人(通过过滤
array_length()
>= 1)
- 获取所有议程标识符,这样我就可以在后续查询中获取它们的信息而无需再次过滤(在我上面的示例中的字段
agenda.name
上)(通过将数组投影到 SELECT
条款)
第一个项目符号可以通过简单的连接来完成。但是,对于结合第二个项目符号的第一个项目符号,我需要在议程标识符上进行某种聚合。我认为数组对此很有用。
编辑
根据用户 Saba 的说法,这是不可能的。感谢您的反馈。
下面的查询是一个好的选择吗?
select person.identifier, person.name, array_agg(agenda.identifier)
from person, person_agenda, agenda
where person.identifier = person_identifier and
agenda.identifier = agenda_identifier and
agenda.name = '...'
group by person.identifier;
WHERE 子句中不能使用别名,如果要在WHERE 子句中使用别名,需要将其包装在子查询或CTE 中。因为查询将按以下顺序执行:
FROM
ON
JOIN
**WHERE**
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
**SELECT**
DISTINCT
ORDER BY
TOP
尝试这样的事情:
SELECT
FROM (
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p
) AS per
where array_length(r) >= 1;
您的情况看起来很简单,下面的情况应该适合您:
select person.identifier, person.name, array_agg(agenda.identifier)
from person, person_agenda, agenda
where person.identifier = person_identifier and
agenda.identifier = agenda_identifier and
agenda.name = '...'
group by person.identifier, person.name;
我有以下两个table:
create table person
(
identifier integer not null,
name text not null,
age integer not null,
primary key(identifier)
);
create table agenda
(
identifier integer not null,
name text not null,
primary key(identifier)
);
他们加入了以下 table:
create table person_agenda
(
person_identifier integer not null,
agenda_identifier integer not null,
primary key(person_identifier, agenda_identifier),
foreign key(person_identifier) references person(identifier),
foreign key(agenda_identifier) references agenda(identifier)
);
我试图在 WHERE
子句中引用 SELECT
子句中定义的数组。
以下作品:
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p;
这不是:
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p
where array_length(r, 1) >= 1;
它说 r
不是已知列。如何在 WHERE
子句中引用此数组?
我第二次查询的目的是:
- 忽略没有议程的人(通过过滤
array_length()
>= 1) - 获取所有议程标识符,这样我就可以在后续查询中获取它们的信息而无需再次过滤(在我上面的示例中的字段
agenda.name
上)(通过将数组投影到SELECT
条款)
第一个项目符号可以通过简单的连接来完成。但是,对于结合第二个项目符号的第一个项目符号,我需要在议程标识符上进行某种聚合。我认为数组对此很有用。
编辑
根据用户 Saba 的说法,这是不可能的。感谢您的反馈。
下面的查询是一个好的选择吗?
select person.identifier, person.name, array_agg(agenda.identifier)
from person, person_agenda, agenda
where person.identifier = person_identifier and
agenda.identifier = agenda_identifier and
agenda.name = '...'
group by person.identifier;
WHERE 子句中不能使用别名,如果要在WHERE 子句中使用别名,需要将其包装在子查询或CTE 中。因为查询将按以下顺序执行:
FROM
ON
JOIN
**WHERE**
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
**SELECT**
DISTINCT
ORDER BY
TOP
尝试这样的事情:
SELECT
FROM (
select identifier, name, array(select identifier from agenda a, person_agenda pa where person_identifier = p.identifier and identifier = agenda_identifier and name = '...') as r
from person p
) AS per
where array_length(r) >= 1;
您的情况看起来很简单,下面的情况应该适合您:
select person.identifier, person.name, array_agg(agenda.identifier)
from person, person_agenda, agenda
where person.identifier = person_identifier and
agenda.identifier = agenda_identifier and
agenda.name = '...'
group by person.identifier, person.name;