使用成对值的查找行并找到确切的匹配组
我有这些表:
项目
ID | 名称 | 版本 |
---|---|---|
1 | pete | 0.0.1 |
2 | 赃物 | 0.0.1 |
3 | 赃物 | 0.0.2 |
4 | 赃物 | 0.0.3 |
5 | 羽衣甘蓝 | 0.0.1 |
6 | 羽衣甘蓝 | 0.0.2 |
人
id | 名称 |
---|---|
1 | Jake |
2 | Skye |
3 | Kieth |
4 | Jim |
5 Eliz 5 | Eliz |
Person_Project ID
Person_Id | Project_id | 1 |
---|---|---|
1 | 1 | 1 |
1 | 3 | 2 |
3 | 2 2 1 | 2 |
4 | 5 | 1 |
3 1 5 3 | 3 | 3 3 |
6 | 4 | 1 |
7 | 4 4 4 4 | 4 |
8 5 | 5 | 1 |
9 | 5 | 2 |
10 | 5 | 5 |
人将拥有独特的项目,这意味着没有两个人会在同一项目上工作。
我正在编写一个基于Java的API,其中我收到一个带有不同项目的JSON请求,我必须退还正在从事请求确切项目的人。
请求:
[
{"name": "Pete", "version": "0.0.1"}
]
这应该返回Jake
请求:
[
{"name": "Pete", "version": "0.0.1"},
{"name": "Swag", "version": "0.0.1"}
]
这应该返回Skye
请求:
[
{"name": "Pete", "version": "0.0.1"},
{"name": "Swag", "version": "0.0.2"}
]
这应该返回kieth
我正在为此编写SQL,而没有得到我的东西需要。
这就是我所能做到的,
SELECT pe.id, pe.name
FROM person pe
LEFT JOIN person_project pepr on pepr.person_id = pe.id
WHERE pe.id IN (
SELECT pepr.person_id
FROM project pr
LEFT JOIN person_project pepr ON pepr.project_id = pr.id
WHERE pr.name IN ('Pete', 'Swag') AND pr.version IN ('0.0.1', '0.0.2')
GROUP BY pepr.project_id
HAVING COUNT(pepr.project_id) = 2
)
GROUP BY pe.id, pe.name
HAVING COUNT(pe.id) = 2
这是不正确的,因为我在用于其他项目的版本中使用。
I have these tables:
Project
id | name | version |
---|---|---|
1 | Pete | 0.0.1 |
2 | Swag | 0.0.1 |
3 | Swag | 0.0.2 |
4 | Swag | 0.0.3 |
5 | Kale | 0.0.1 |
6 | Kale | 0.0.2 |
Person
id | name |
---|---|
1 | Jake |
2 | Skye |
3 | Kieth |
4 | Jim |
5 | Eliz |
Person_Project
id | person_id | project_id |
---|---|---|
1 | 1 | 1 |
2 | 2 | 1 |
3 | 2 | 2 |
4 | 3 | 1 |
5 | 3 | 3 |
6 | 4 | 1 |
7 | 4 | 4 |
8 | 5 | 1 |
9 | 5 | 2 |
10 | 5 | 5 |
Persons will have unique projects, means no two persons will work on the same projects.
I am writing a java based api where I receive a json request with different projects and I have to return the person who is working on exact projects given in request.
Request:
[
{"name": "Pete", "version": "0.0.1"}
]
This should return Jake
Request:
[
{"name": "Pete", "version": "0.0.1"},
{"name": "Swag", "version": "0.0.1"}
]
This should return Skye
Request:
[
{"name": "Pete", "version": "0.0.1"},
{"name": "Swag", "version": "0.0.2"}
]
This should return Kieth
I am writing SQL for this and not getting what I needed.
This is what I am up to
SELECT pe.id, pe.name
FROM person pe
LEFT JOIN person_project pepr on pepr.person_id = pe.id
WHERE pe.id IN (
SELECT pepr.person_id
FROM project pr
LEFT JOIN person_project pepr ON pepr.project_id = pr.id
WHERE pr.name IN ('Pete', 'Swag') AND pr.version IN ('0.0.1', '0.0.2')
GROUP BY pepr.project_id
HAVING COUNT(pepr.project_id) = 2
)
GROUP BY pe.id, pe.name
HAVING COUNT(pe.id) = 2
This is not right as I am using IN for versions which will gets applied to other projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以如此答案。您对说明确切的分区/无剩余部分感兴趣:
You can use SQL relational division logic as described in this answer. You're interested in the part that says exact division/no remainder:
DB<>Fiddle for all three examples
这是一个经典关系划分无剩余的问题问题。
首先将输入数据放置在表变量或表值参数或temp中桌子。
然后,您可以使用标准关系划分答案之一
这要做的就是以下内容:
存在
子查询为真:person> person
他们的所有person_project
行project
)。还有其他解决方案。
This is a classic Relational Division Without Remainder question.
Start by placing your input data into a table variable or a Table Valued Parameter or a temp table.
Then you can use one of the standard relational division answers
db<>fiddle
What this does is the following:
Person
rows where the followingEXISTS
subquery is true:Person
take all theirPerson_Project
rowsProject
at the same time).There are other solutions to this also.