简体   繁体   English

使用具有一对多关系的连接最小化SQL查询

[英]Minimizing SQL queries using join with one-to-many relationship

So let me preface this by saying that I'm not an SQL wizard by any means. 因此,让我先说一下,我不是一个SQL向导。 What I want to do is simple as a concept, but has presented me with a small challenge when trying to minimize the amount of database queries I'm performing. 我想要做的是作为一个概念简单,但在尝试最小化我正在执行的数据库查询量时给了我一个小小的挑战。

Let's say I have a table of departments. 假设我有一个部门表。 Within each department is a list of employees. 每个部门都有一份员工清单。

What is the most efficient way of listing all the departments and which employees are in each department. 列出所有部门以及每个部门中哪些员工的最有效方式是什么。

So for example if I have a department table with: 因此,例如,如果我有一个部门表:

id   name
1    sales
2    marketing

And a people table with: 还有一张人员表:

id   department_id   name
1    1               Tom
2    1               Bill
3    2               Jessica
4    1               Rachel
5    2               John

What is the best way list all departments and all employees for each department like so: 列出所有部门和每个部门所有员工的最佳方式是这样的:

Sales 销售

  • Tom 汤姆
  • Bill 法案
  • Rachel 雷切尔

Marketing 营销

  • Jessica 杰西卡
  • John 约翰

Pretend both tables are actually massive. 假装两张桌子实际上都很庞大。 (I want to avoid getting a list of departments, and then looping through the result and doing an individual query for each department). (我想避免获取部门列表,然后循环遍历结果并为每个部门执行单独查询)。 Think similarly of selecting the statuses/comments in a Facebook-like system, when statuses and comments are stored in separate tables. 当状态和注释存储在单独的表中时,类似地考虑在类似Facebook的系统中选择状态/注释。

You can get it all in a single query with a simple join, eg: 您可以使用简单连接在单个查询中获取所有内容,例如:

SELECT   d.name AS 'department', p.name AS 'name'
FROM     department d
  LEFT JOIN people p ON p.department_id = d.id
ORDER BY department

This returns all the data, but it's a bit of a pain to consume, since you'll have to iterate through every person anyway. 这将返回所有数据,但消耗起来有点痛苦,因为无论如何你都必须遍历每个人。 You can go further and group them together: 你可以进一步将它们组合在一起:

SELECT   d.name AS 'department',
         GROUP_CONCAT(p.name SEPARATOR ', ') AS 'name'
FROM     department d
  LEFT JOIN people p ON p.department_id = d.id
GROUP BY department

You'll get something like this as the output: 你会得到这样的东西作为输出:

department | name
-----------|----------------
sales      | Tom, Bill, Rachel
marketing  | Jessica, John
SELECT d.name, p.name
FROM department d
JOIN people p ON p.department_id = d.id

I suggest also reading a SQL Join tutorial or three. 我建议还阅读SQL Join教程或三个。 This is a very common and very basic SQL concept that you should understand thoroughly. 这是一个非常常见且非常基本的SQL概念,您应该彻底了解它。

This is normally done in a single query: 这通常在单个查询中完成:

SELECT DepartmentTable.Name, People.Name from DepartmentTable 
INNER JOIN People
ON DepartmentTable.id = People.department_id
ORDER BY DepartmentTable.Name

This will suppress empty departments. 这将压制空部门。 If you want to show empty departments, change INNER to LEFT OUTER 如果要显示空部门,请将INNER更改为LEFT OUTER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM