我有一个名为'vacancycies'的表有一个'vacancy_id'PK。它看起来像这样:使用不同表格和过滤器的MySQL查询
create table vacancies
(
vacancy_id int not null auto_increment,
org_id int not null,
name varchar(255) not null comment 'title',
vacancy_visibility_start_date datetime comment 'vacancy visibility date, when it needs to be active on the website',
vacancy_visibility_end_date datetime,
primary key (vacancy_id)
);
在此之后,我有几个其他表链接到这一个。
create table vacancy_calendar
(
vacancy_calendar_id int not null auto_increment,
vacancy_id int,
date_from datetime not null,
date_to datetime not null,
primary key (vacancy_calendar_id)
);
create table vacancy_interests
(
vacancy_id int,
interest_id int
);
create table vacancy_skills
(
vacancy_id int,
skill_id int
);
所有这些表可以包含同一个vacancy_id的多行。
我的页面有不同的过滤器,我想通过AJAX进行处理。 我希望每个空位都有一行包含我需要的所有数据+它必须符合我的过滤标准。然而,我不知道我的查询如何看起来像检索我正在寻找的结果。 可以根据'interest_id','skill_id','date_from'和'date_to'进行过滤。
我开始用下面的查询,但我坚持非常快:
SELECT v.*, vi.interest_id
FROM `vacancies` as v
INNER JOIN `vacancy_interests` as vi on v.vacancy_id = vi.vacancy_id
GROUP BY v.vacancy_id
这个查询只能返回我1个interest_id的空缺,即使有空位的vacancy_interest表3个interest_id行。如果我删除了GROUP BY语句,我会得到3行的空缺,这不是我想要的。
理想情况下,我想将interest_id分别放在单独的列中或由逗号分隔的相同字段中。或者如果有任何其他可能性/建议,请随时分享!
作为一个后续问题:如果我想在WHERE语句中添加一些过滤,如下WHERE vsskill_id IN(4)和vi.interest_id IN(1,3)。我该如何解决这个问题?这样做会使得group_concat字段总是只返回在where子句中传递的整数。 – Dennis
@ Dennis ...我已经更新了答案.. – scaisEdge
这不起作用,正如我在评论中提到的。考虑我有一行,其中skill_id列包含值“1,4,8”(因为group_concat)。如果使用我的查询与vs.skill_id IN(4)where子句,vs.skill_id列将只返回我4,而我也想保留在那里的1和8值 – Dennis