编辑更新:原来我有 5.7 版,所以 Window Functions 不是找到解决方案的选项。
SHOW VARIABLES LIKE 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.7.21-log |
+---------------+------------+
问题描述:我有一个offer、skills和profiles之间的三元关系表。这种三元关系有一个属性,排名。
我有一个技能表,我可以在其中看到技能的名称。到目前为止,我必须做两个查询:
1) 给我每个个人资料排名前 10 位的技能:
SELECT DISTINCT ternary.id_skill, skill.name_skill, ranking_skill
FROM ternary
INNER JOIN skill ON skill.id_skill=ternary.id_skill
WHERE ternary.id_perfil= #IntNumber#
GROUP BY ternary.id_skill
ORDER BY ternary.ranking_skill DESC
LIMIT 10;
2) 对于 ID 技能列表,请告诉我它们是否出现在任何个人资料中,以及它们出现的次数。
SELECT DISTINCT ternary.id_profile, nombre_profile, COUNT(DISTINCT ternary.id_skill) AS matching
FROM ternary
INNER JOIN profile ON ternary.id_profile=profile.id_profile
WHERE ternary.id_skill= '858534430'
OR ternary.id_skill= '3213227'
OR ternary.id_skill= '3254818'
GROUP BY(ternary.id_profile)
ORDER BY matching DESC;
在最后一个查询中,已经确定了一个问题:它“搜索”在个人资料的任何点出现的技能。由于个人资料可能拥有数千种技能,因此可能会产生误导,因为我们想要实现的目标,我现在只需要在它是任何个人资料的前 10 项技能之一时“搜索”。但只进前十。
到目前为止,基本上我一直在尝试混合这两个查询,但收效甚微,因为似乎我无法对两列进行分区,即使我只使用一个,我也会得到You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(PARTITION BY
:
SELECT *
FROM
(
SELECT DISTINCT ternary.id_skill,
skill.name_skill,
ternary.ranking_skill,
ternary.id_profile,
ROW_NUMBER() OVER(PARTITION BY id_profile, id_skill ORDER BY ternary.ranking_skill DESC) rn
FROM ternary
INNER JOIN skill ON skill.id_skill=ternary.id_skill
)
WHERE rn < 11;
我了解到这个操作可能被称为分组最大值,我已经看到了几个寻找这个的答案。我无法复制它们中的任何一个,mysql Ver 14.14 Distrib 5.5.60, for Linux (x86_64) using readline 5.
如果它有任何帮助,我特别需要它(我已经尝试过对其他一些类似数据库来说非常完美但在 mysql 中不起作用的答案)。
表定义:
CREATE TABLE `ternary` (
`id_offer` varchar(200) NOT NULL,
`id_skill` varchar(200) NOT NULL,
`id_profile` varchar(200) NOT NULL,
`ranking_skill` double NOT NULL,
PRIMARY KEY (`id_offer`,`id_skill`,`id_profile`),
KEY `id_skill` (`id_skill`),
KEY `id_profile` (`id_profile`),
CONSTRAINT `ternary_ibfk_1` FOREIGN KEY (`id_offer`) REFERENCES `offer` (`id_offer`),
CONSTRAINT `ternary_ibfk_2` FOREIGN KEY (`id_skill`) REFERENCES `skill` (`id_skill`),
CONSTRAINT `ternary_ibfk_3` FOREIGN KEY (`id_profile`) REFERENCES `profile` (`id_profile`)
)
CREATE TABLE `skill` (
`id_skill` varchar(200) NOT NULL,
`name_skill` varchar(200) DEFAULT NULL,
`date` date DEFAULT NULL,
PRIMARY KEY (`id_skill`)
)
做的结果
select * from ternay limit 10;
+------------+------------+-----------+----------------------+
| id_oferta | id_skill | id_perfil | ranking_skill |
+------------+------------+-----------+----------------------+
| 1004 | 107 | 679681082 | 0 |
| 1004 | 115 | 679681082 | 0.10846866454897801 |
| 1004 | 117 | 679681082 | 0.038003619695992294 |
| 1004 | 129 | 679681082 | 0.04987975085098989 |
| 1004 | 147 | 679681082 | 0.02771097269499438 |
| 1004 | 299 | 679681082 | 0.0522549770819894 |
| 1004 | 321 | 679681082 | 0.11955305362697576 |
| 1004 | 417 | 679681082 | 0.11321911701097703 |
| 1004 | 964 | 679681082 | 0.015043099462996949 |
| 1004 | 967 | 679681082 | 0.05304671915898924 |
+------------+------------+-----------+----------------------+
查询 1) 的结果如上所述,它为我提供了 ONE 配置文件的前 10 名
+------------+--------------+---------------------+
| id_skill | name_skill | ranking_skill |
+------------+--------------+---------------------+
| 109 | scala | 0.3089840175329823 |
| 122 | hadoop | 0.24164146109602963 |
| 9731 | python | 0.21470443852124863 |
| 325 | java | 0.18776741594646754 |
| 114 | sql | 0.14736188208429596 |
| 101 | kafka | 0.13389337079690544 |
| 301 | bbdd | 0.13389337079690544 |
| 927 | agile | 0.13389337079690544 |
| 320 | hive | 0.1204248595095149 |
| 109 | spark | 0.1204248595095149 |
+------------+--------------+---------------------+