gpt4 book ai didi

MySQL查询加入

转载 作者:行者123 更新时间:2023-11-29 04:21:55 25 4
gpt4 key购买 nike

我有一个 MySQL 问题。与this other question that I asked previously有关, 但略有不同。

我有 2 个表:团队、承包商

TEAMS
|team_id | location|
--------------------
| 1 | space1 |
--------------------
| 2 | space2 |
--------------------
| 3 | space3 |
--------------------
| 4 | space3 |
--------------------

CONTRACTORS
|cont_id | location| team_id|
-----------------------------
| 1 | space1 | 1 |
-----------------------------
| 2 | space1 | 0 |
-----------------------------
| 3 | space3 | 3 |
-----------------------------
| 4 | space3 | 3 |
-----------------------------
| 5 | space3 | 0 |
-----------------------------
| 6 | space3 | 4 |
-----------------------------

如果我有一个 team_id,我想创建一个查询来查找位于给定 team_id 位置并且仅属于给定 team_id 或具有 team_id = 0 的所有承包商. (CONTRACTORS 表中的 team_id = 0 表示承包商当前不属于团队)。

例如,我得到了 team_id = 3.team_id = 3的团队位于space3。我想找到位于 space3 且仅属于 team_id = 3 的承包商或 team_id = 0 (本例中的 cont_id = 3、4、5)。

有没有办法通过单个 MySQL 查询来实现这一点?

最佳答案

试试这个:

select cont_id, c.team_id, c.location 
from CONTRACTORS c join TEAM t
on c.team_id = t.team_id
where c.team_id = GIVEN_TEAM_ID or c.team_id = 0

关于MySQL查询加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21475718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com