我有这三张桌子
CREATE TABLE "Countries"
(
name text NOT NULL,
pop integer,
CONSTRAINT country PRIMARY KEY (name),
CONSTRAINT pop_check CHECK (pop > 0)
)
CREATE TABLE "Cities"
( name text NOT NULL, pop integer,
country_name text,
CONSTRAINT "City_ref" PRIMARY KEY (name),
CONSTRAINT country_ref FOREIGN KEY (country_name)
REFERENCES "db1001002_Countries" (name) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT "Population" CHECK (pop > 0)
);
CREATE TABLE " Company"
(
"City_name" text NOT NULL,
"Company_Name" text NOT NULL,
no_of_employee integer,
CONSTRAINT "City_company" PRIMARY KEY ("City_name", "Company_Name"),
CONSTRAINT city_ref FOREIGN KEY ("City_name")
REFERENCES "Cities" (name) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT employee_check CHECK (no_of_employee > 0)
)
我的问题是我想使用格式的嵌套查询
select field(s) from table(s)
where field(s) in (select field(s) from table(s) condition)
group by field(s)
获取包含字段的新表
country, min_pop, max_pop, average_pop
for cities with pop > 500000
我尝试编写一些查询,但在您的子查询中不断收到太多字段的错误。我正在使用 postgresql 9.3。
当我使用这个查询时,我得到了一些结果。
SELECT country_name, min_pop, max_pop, avg_popo
FROM (SELECT country_name, min(pop),max(pop),avg(pop)
FROM
"Cities"
GROUP BY country_name)
AS popcal(country_name,min_pop, max_pop, avg_popo)
WHERE min_pop>500000
有没有办法以这种格式编写查询
select field(s) from table(s)
where field(s) in (select field(s) from table(s) condition)
group by field(s)
?