简体   繁体   English

在 MySQL 上按条件进行唯一连接

[英]Unique join by condition on MySQL

I have a table containing employees and in this table I have following data:我有一个包含员工的表,在这个表中我有以下数据:

id          username   employee_status
33189       SLEE       3
36351       SLEE       0
29096595    SLEE       0
19197668    AARM       0
20738021    AARM       0
29097305    AARM       0

While employee status 3 means active, 0 means non-active.员工状态 3 表示活跃,0 表示非活跃。 So this data means user SLEE has been transferred couple of times and still working in the company and user AARM has been transferred couple of times and left the company.所以这个数据意味着用户 SLEE 已经被转移了几次并且仍然在公司工作,而用户 AARM 已经被转移了几次并离开了公司。

What I need is to distinct join on this table.我需要的是在这张桌子上进行不同的联接。 I could write a query like that:我可以写一个这样的查询:

SELECT username
    , MAX(id)
FROM employees
GROUP BY username;

But the problem here is having maximum id doesn't always mean that is the active user (if there is one) has always the maximum id as you can see in the user SLEE.但这里的问题是拥有最大 id 并不总是意味着活动用户(如果有的话)总是拥有最大 id,正如您在用户 SLEE 中看到的那样。

I need to write a conditional join, if there is a user with employee status 3 return that otherwise return maximum id.我需要编写一个条件连接,如果有一个员工状态为 3 的用户返回,否则返回最大 id。 Something like this query:像这样的查询:

SELECT userid
    , IF(employeestatus = 3 THEN id ELSE MAX(id) END)
FROM employees
GROUP BY userid;

But this query doesn't work.但是这个查询不起作用。 Expected result is:预期结果是:

id          username   employee_status
33189       SLEE       3
29097305    AARM       0

You can use conditional ordering with correlated sub-query :您可以将条件排序与相关子查询一起使用:

select e.*
from de_ie_cm.employees e
where e.id = (select e1.id
              from de_ie_cm.employees e1
              where e1.userid = e.userid
              order by (case when employeestatus = 3 then 0 else 1 end), id desc
              limit 1
             );

If you are using MySQL version 8.0+ then this code will work for you.如果您使用的是 MySQL 8.0+ 版,那么此代码将适用于您。

    SELECT ID,username,Employee_Status FROM 
    (
        SELECT *,ROW_NUMBER() OVER (PARTITION BY username ORDER BY Final_ID DESC) Rw_ID FROM 
        (
            SELECT ID,username,employee_Status,CASE WHEN Employee_Status = 3 THEN  2 ELSE 0 END + CASE WHEN R_ID = 1 THEN 1 ELSE 0 END AS Final_ID
            FROM 
                (
                    SELECT ROW_NUMBER() OVER (PARTITION BY username ORDER BY CAST(Id AS INT) DESC) AS R_ID ,* 
                    FROM  employees
                )A
        )A
    ) B WHERE Rw_ID = 1

If you want the id where the status is 3 and the max id otherwise, then you can use conditional aggregation:如果你想在id ,其中状态为3和最大id ,否则,那么你可以使用条件汇总:

SELECT userid, MAX(employeestatus) as employee_status,
       COALESCE(MAX(CASE WHEN employeestatus = 3 THEN id END),
                MAX(id)
               )
FROM employees
GROUP BY userid;

I think this should work for you我认为这应该适合你

SELECT (CASE
            WHEN max(status) = 0 THEN max(id)
            WHEN max(status) = 3 THEN
                   (SELECT id
                    FROM A a2
                    WHERE status = 3
                      AND a1.name = a2.name)
        END) AS aggregate_id,
       name
FROM A a1
GROUP BY name

DBFiddle DB小提琴

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

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