我目前已经定义了这样的视图
CREATE VIEW top10 as
(
SELECT distinct ternary.id_skill, skill.name_skill, ranking_skill, ternary.id_profile, profile.name_profile
FROM ternary
INNER JOIN skill
ON skill.id_skill=ternary.id_skill
INNER JOIN profile
ON profile.id_profile=ternary.id_profile
WHERE ternary.id_profile= 1
GROUP BY ternary.id_skill
ORDER BY ternary.ranking_skill DESC
LIMIT 10
)
UNION
(
SELECT distinct ternary.id_skill, skill.name_skill, ranking_skill, ternary.id_profile, profile.name_profile
FROM ternary
INNER JOIN skill
ON skill.id_skill=ternary.id_skill
INNER JOIN profile
ON profile.id_profile=ternary.id_profile
WHERE ternary.id_profile= 2
GROUP BY ternary.id_skill
ORDER BY ternary.ranking_skill DESC
LIMIT 10
)
UNION
(
........
)
如您所见,View只是在N个查询之间进行联合,除了查询不同的id_profile
之外,其他所有查询都是相同的,并且每个查询给我的个人资料前10名(从第一个到最后一个)到现在的22。定义后,我将像以前一样查询视图,例如使用
select * from top10
或insert way more complex query here
现在,我想使这种相同的解决方案在不对视图中的配置文件进行硬编码的情况下工作,因为可以添加甚至更改更多的配置文件。因此,我构建了以下查询,我希望将其替换为以前的View,但不会给出相同的结果,因此,我正在寻找一种适当的SQL方式来执行此操作,正如我所说的那样,没有查询该动态地受定义了多少个配置文件的影响。
SELECT distinct ternary.id_skill, skill.name_skill, ranking_skill, ternary.id_profile, profile.name_profile
FROM ternary
INNER JOIN skill
ON skill.id_skill=ternary.id_skill
INNER JOIN profile
ON profile.id_profile=ternary.id_profile
WHERE ternary.id_profile IN (SELECT id_profile from profile)
GROUP BY ternary.id_skill
ORDER BY ternary.ranking_skill DESC
LIMIT 10;
在有用的情况下定义表:
CREATE TABLE profile (
id_profile VARCHAR(200) NOT NULL,
name_profile VARCHAR(200),
type_profile VARCHAR(200),
PRIMARY KEY (id_profile)
);
CREATE TABLE ternary (
id_oferta VARCHAR(200) NOT NULL,
id_skill VARCHAR(200) NOT NULL,
id_profile VARCHAR(200) NOT NULL,
ranking_skill DOUBLE NOT NULL,
PRIMARY KEY (id_oferta, id_skill, id_profile),
FOREIGN KEY (id_oferta) REFERENCES oferta(id_oferta),
FOREIGN KEY (id_skill) REFERENCES skill(id_skill),
FOREIGN KEY (id_profile) REFERENCES profile(id_profile)
);
答案 0 :(得分:-1)
尝试一下
SELECT ternary.id_skill,
skill.name_skill,
ranking_skill,
ternary.id_profile,
profile.name_profile
FROM ternary
INNER JOIN skill ON skill.id_skill=ternary.id_skill
INNER JOIN profile ON profile.id_profile=ternary.id_profile
WHERE (ternary.id_profile, ternary.id_skill) in (Select distinct id_profile, id_skill From ternary where id_profile between 1 and 22)
ORDER BY ternary.ranking_skill DESC
注意:我假设所需的id_profile为1到22。如果不是这样,则将其更改