MySQL关系数据库查询,术语正确吗?

发布于 2024-11-27 22:08:46 字数 721 浏览 0 评论 0原文

我认为我的数据库问题源于不知道正确的术语来帮助我自己找到答案,所以我将解释我正在做的事情的通用版本,希望你能指出一些教程或给我一些术语来检查进入。

让我们使用员工目录的示例。

每个员工可以有多个地点、多个工作职责,这些都来自一个单独的表。示例表和一些数据,让我们只关注多个位置。

员工
主要员工数据
- ID(例如:400)
- 第一个(例如:约翰)
- 最后(例如:Doe)

地点
位置的唯一列表
- ID(例如:3000)
- 头衔(例如:FakeCo, LLC)

ma​​p_employees_locations
将任意数量的地点与员工绑定
- 身份证号
- 员工 ID(例如:400)
- location_id(例如:3000)

我正在努力解决单个查询如何返回如下内容的逻辑:

约翰
美国能源部
FakeCo 有限责任公司
另一个公司

看来我必须运行一个查询来获取员工数据,然后在嵌套查询中,获取与员工 ID 关联的位置等...如果每个员工只有一个位置,那么这将是一个简单的联接,我只是不知道如何处理倍数。

如果我还很遥远,请告诉我,我只是在挣扎。

I think my issue with databases stems from not knowing the correct terminology to help me find an answer myself so I'll explain a generic version of what I'm doing and hopefully you can point some tutorials my way or give me some terms to check into.

Let's use an example of an employee directory.

Each employee can have multiple locations, multiple job duties which pull from a separate table. Example tables & some data, let's just focus on the multiple locations.

employees
Main employee data
- id (ex: 400)
- first (ex: John)
- last (ex: Doe)

locations
Unique list of locations
- id (ex: 3000)
- title (ex: FakeCo, LLC)

map_employees_locations
Tie ANY number of locations to an employee
- id
- employee_id (ex: 400)
- location_id (ex: 3000)

I'm struggling with the logic of how a single query would return something like this:

John
Doe
FakeCo, LLC
AnotherCo, LLC

It seems I would have to run a query to get the employee data, then within a nested query, grab locations associated with the employee id, etc... If there was only one location per employee, it would be a simple join, I just don't know how to handle the multiples.

Let me know if I'm way off, I'm just struggling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-12-04 22:08:46

您可以像这样将所有表连接在一起

select e.id,e.first,e.last,l.id,l.title
from employees e
inner join map_employees_locations el
on el.employee_id = e.id
inner join locations l
on el.location_id = l.id
where e.first = 'John'
AND e.last = 'Doe'

这将返回如下数据:

e.id    e.first   e.last   l.id    l.title
------------------------------------------------
1       John      Doe      1       FakeCo, LLC
1       John      Doe      2       AnotherCo, LLC

You would join all of the tables together like this

select e.id,e.first,e.last,l.id,l.title
from employees e
inner join map_employees_locations el
on el.employee_id = e.id
inner join locations l
on el.location_id = l.id
where e.first = 'John'
AND e.last = 'Doe'

This would return data like this:

e.id    e.first   e.last   l.id    l.title
------------------------------------------------
1       John      Doe      1       FakeCo, LLC
1       John      Doe      2       AnotherCo, LLC
听闻余生 2024-12-04 22:08:46

如果您只想每个员工一行,您可能应该使用 group concat

select id, e.last, e.first
     group_concat(l.title separator ',' ) as locations 
from employee e
join location l on l.employee_id = e.id
group by e.id

不确定语法,因为我更了解 postgres 但这应该可以完成这项工作。

If you want only one line per employee you should maybe use group concat

select id, e.last, e.first
     group_concat(l.title separator ',' ) as locations 
from employee e
join location l on l.employee_id = e.id
group by e.id

Not sure about the syntax cos i'm more aware of postgres but this should do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文