简体   繁体   English

在MySQL中将结果分组

[英]Grouping results in MySQL

My brain is turning into tapioca trying to figure this one out. 我的大脑正变成木薯粉,试图弄清楚这一点。 I have a table of subordinates and supervisors. 我有一张下属和主管的表。 Every employee has a number of course. 每个员工都有很多课程。 Here's an example. 这是一个例子。

I have three fields: employee_id, name, supervisor_id 我有三个字段:employee_id,姓名,supervisor_id

Fred Wilkie is a supervisor and his record is .... 弗雷德·威尔基(Fred Wilkie)是主管,他的记录是....

employee_id: 1
name: Fred Wilkie
supervisor_id: NULL 

Ted Wilkie is a lowly worker and Fred is his boss. 泰德·威尔基(Ted Wilkie)是一个低下的工人,弗雷德(Fred)是他的老板。 His entry looks like this.... 他的条目看起来像这样。

employee_id: 2
name: Ted Wilkie
supervisor_id: 1

What I would like my query to look like is employee_id, name and supervisor_id but the supervisor_id should be equal to the employee_id if the supervisor_id is NULL. 我希望查询显示的是employee_id,名称和supervisor_id,但是如果supervisor_id为NULL,则supervisor_id应该等于employee_id。

This kinda works... ( I think I just need to refine it somehow) 这有点工作...(我想我只需要以某种方式对其进行改进)

select employee_id, name, supervisor_id case when supervisor_id is NULL then employee_id else supervisor_id end from employees order by supervisor_id;

Problem with this is that it orders all of the records first where the employee_id is equal to the supervisor_id and then it just spits out the subordinates that are left.... 问题在于,它首先对所有记录进行排序,其中employee_id等于supervisor_id,然后仅吐出剩下的下属。

employee_id, name, supervisor_id
1, Fred Wilkie, 1
4, Gail Winston, 4
2, Ted Wilkie, 1
3, Lisa Wilkie, 1
5, Russ Cablas, 4
6, Ben Reynolds, 4
etc, etc, etc...

what I would like is this...... 我想要的是这个……

employee_id, name, supervisor_id
1, Fred Wilkie, 1
2, Ted Wilkie, 1
3, Lisa Wilkie, 1
4, Gail Winston, 4
5, Russ Cablas, 4
6, Ben Reynolds, 4
etc, etc, etc...

In the example above I have the the first supervisor (Fred) listed (employee_id = supervisor_id) and then all of his subordinates. 在上面的示例中,我列出了第一个主管(Fred)(employee_id = supervisor_id),然后列出了他的所有下属。 After that is Gail, and all of her subordinates, etc. I think my group-fu is weak. 之后是盖尔(Gail)和她的所有下属,等等。我认为我的傅赋很弱。

We run a large company (250 employees) so we'd like a way to keep this in the MySQL logic. 我们经营着一家大公司(250名员工),因此我们希望有一种方法可以将其保留在MySQL逻辑中。 Does anyone have an idea? 有人有主意吗?

Many thanks! 非常感谢! Janie 珍妮

The easiest solution on this is to use COALESCE 最简单的解决方案是使用COALESCE

SELECT  employee_ID,
        name,
        COALESCE(supervisor_id, employee_id) AS supervisor_id
FROM    employees
ORDER BY supervisor_id, employee_ID

SQLFiddle Demo SQLFiddle演示

additional query ( if you want to get their supervisor name instead of id ) 其他查询( 如果要获取其主管名称而不是id

SELECT  a.employee_ID,
        a.name,
        COALESCE(b.name, a.name) AS Supervisor_Name
FROM    employees a
        LEFT JOIN employees b
          ON a.supervisor_ID = b.employee_ID
ORDER BY COALESCE(b.supervisor_id, a.employee_id), employee_ID

SQLFiddle Demo SQLFiddle演示

select employee_id, name, supervisor_id case when supervisor_id is NULL then employee_id else supervisor_id end from employees order by supervisor_id ASC;

应该做到这一点...

i think you are missing the sequence to get records you are using order by but not ponting the sequence ASC or DESC 我认为您缺少通过order by而不是顺序ASCDESC来获取使用order by记录的顺序

select employee_id, name, 
supervisor_id case when supervisor_id is NULL 
then employee_id else supervisor_id end 
from employees 
order by supervisor_id
ASC;

hope this works 希望这有效

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

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