这个问题有点模糊,我道歉,希望下面的例子能够清除它。这是一个相当基本的问题,我似乎无法通过我非常有限的知识和SQL相关词汇找到合适的解决方案
有一张人的桌子,
create table People (
id integer,
name LongName,
primary key (id)
);
一个引用人的工人
create table Workers (
id integer references People(id),
worktype varchar(20),
primary key (id)
);
最后是一个works_for关系
create table Works_for (
worker integer references Workers(id),
employer integer references Job(id),
primary key (worker,job)
);
现在我要做的是让所有工作至少20个工作的人,所以我使用以下查询得到正确的id列表:
SELECT worker
FROM Works_for
GROUP BY worker
HAVING COUNT(worker) > 20;
但是我也希望得到这些工人的名字。我该怎么做?我尝试过很多东西但是我一直遇到错误。任何帮助将不胜感激!
答案 0 :(得分:3)
您可以加入表格并选择以下两个字段:
SELECT p.name, p.id
FROM People p
JOIN Works_for wf ON (p.id = wf.worker)
GROUP BY id
HAVING COUNT(wf.worker) > 20;
答案 1 :(得分:1)
SELECT worker,name
FROM Works_for join People on worker=id
GROUP BY worker,name
HAVING COUNT(employer) > 20;
http://sqlfiddle.com/#!15/e03e3/1 将会有20个但只有3个记录,但我认为它足以作为演示
答案 2 :(得分:0)
你可以使用左连接:
select worker, name, worktype from
(select worker,MIN(employer) as employer
from works_for group by worker having COUNT(worker)>20) w
left join People p on p.id = w.employer
left join Workers ws on ws.id = w.worker
像这样,您还可以获得人名和工作类型
答案 3 :(得分:0)
使用当前架构时:
SELECT id, p.name
FROM (
SELECT worker AS id
FROM works_for
GROUP BY 1
HAVING count(*) > 20
) wf
JOIN people p USING (id);
首先聚合并消除不相关的行并然后加入更快。使用EXPLAIN ANALYZE
进行测试。
count(*)
也比count(worker)
快一点。只要worker
不能为NULL,它就会一样。这就是这种情况。
有许多相关的答案:
您呈现的表格布局很奇怪。 workers
表格是people
的1:1扩展名。您也可以将worktype
列添加到people
并删除表格workers
。但是,一个人只能拥有一个worktype
......
通常,它看起来像这样:
CREATE TABLE person (
person_id serial PRIMARY KEY
, name text NOT NULL -- custom domain?
);
CREATE TABLE job (
job_id serial PRIMARY KEY
, name text NOT NULL
-- more
);
CREATE TABLE person_job (
person_id int REFERENCES person
, job_id int REFERENCES job
, worktype_id int REFERENCES worktype
, PRIMARY KEY (person_id, job_id)
);
工作类型分为person_job
或job
表。等等更多: