Mysql 查询具有一行或多行一种状态优先 Mysql 5.7

标签 mysql

我有以下字段:

| employee_email | status | id |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7b6a7c7b3e4f7b6a7c7b216c6062" rel="noreferrer noopener nofollow">[email protected]</a> |  active | 1 | 
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b6a7b1b6f382b6a7b1b6eca1adaf" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 2 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46322335327406322335326825292b" rel="noreferrer noopener nofollow">[email protected]</a> |  active | 3 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1b5a4b2b5f281b5a4b2b5efa2aeac" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 4 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6c2d3c5c282f6c2d3c5c298d5d9db" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 5 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a6b7a1a6e692a6b7a1a6fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 6 |

我需要使用以下规则进行查询:同一员工电子邮件是否有超过 1 行(其中一行处于事件状态,另一行已终止 - 则仅返回行 = 事件)。如果超过 1 行并且都终止,则仅返回终止的一行。

输出应该是:

| employee_email | status | status |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8b9a8c8bcebf8b9a8c8bd19c9092" rel="noreferrer noopener nofollow">[email protected]</a> |  active | 1 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88c9d8b8ccab88c9d8b8cd69b9795" rel="noreferrer noopener nofollow">[email protected]</a> |  active | 3 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5b4a5c5b1c6f5b4a5c5b014c4042" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 4 |
| <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f2e3f5f2b2c6f2e3f5f2a8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a> |  terminated | 5 |

最佳答案

如果您的 MySql 版本是 8.0+,您可以使用窗口函数 ROW_NUMBER():

SELECT t.employee_email, t.status, t.id
FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY employee_email ORDER BY status = 'active' DESC, id) rn
  FROM tablename
) t
WHERE t.rn = 1

对于以前的版本:

SELECT t.employee_email, MAX(t.status) status, MIN(t.id) id
FROM tablename t
WHERE t.status = 'active'
OR NOT EXISTS (SELECT 1 FROM tablename WHERE employee_email = t.employee_email AND status = 'active')
GROUP BY t.employee_email

请参阅demo .
结果:

> employee_email | status     | id
> :------------- | :--------- | -:
> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfabbaacabee9fabbaacabf1bcb0b2" rel="noreferrer noopener nofollow">[email protected]</a> | active     |  1
> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1f0e181f592b1f0e181f45080406" rel="noreferrer noopener nofollow">[email protected]</a> | active     |  3
> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c58495f581f6c58495f58024f4341" rel="noreferrer noopener nofollow">[email protected]</a> | terminated |  4
> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb9a8beb9f98db9a8beb9e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a> | terminated |  5

关于Mysql 查询具有一行或多行一种状态优先 Mysql 5.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64051999/

相关文章:

mysql - 当我通过 Asc Cakephp 2.0 应用订单时,在顶部添加了空值

mysql - SQL 简单连接条件

java - MysqlDataSource 在 Glassfish 中抛出 ClassCastException

mysql - 使用 SQL/Access 创建类似表数据透视的输出

php - 根据条件从 2 个表中选择查询

php - Yii框架无法将数据插入特定表

MySQL - 更新连接和增量

mysql - Entity Framework /SQL - 排除多列数据

mysql - 在rails 3.1RC4上使用mysql

mysql - 从一个表返回另一个表中不存在的值