mysql - 如何在mysql中选择除group by之外的其他列

标签 mysql database mysql-5.7

我实际上有一个像这样的数据库

+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field        | Type             | Null | Key | Default           | Extra                       |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| org_entry_id | int(10) unsigned | NO   | MUL | NULL              |                             |
| check_in     | datetime         | YES  |     | NULL              |                             |
| check_out    | datetime         | YES  |     | NULL              |                             |
| created_at   | datetime         | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at   | datetime         | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| deleted_at   | datetime         | YES  |     | NULL              |                             |
+--------------+------------------+------+-----+-------------------+-----------------------------+

现在我想从这个表中获取 id 字段和 max(check_in) 以及 groupBy org_entry_id

我尝试过这样的查询,但它会给出完整的groupBy模式错误

select id, max(check_in) from time_sheets group by org_entry_id;

所以我阅读了手册,发现它被写入 ANY_VALUE 但我不想要该解决方案。我想要当前 max(check_in) 前面的准确的 id

这是一些示例数据

+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+
| id | org_entry_id | check_in            | check_out           | created_at          | updated_at          | deleted_at |
+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+
|  1 |            3 | 2018-04-03 01:48:07 | NULL                | 2018-04-03 13:53:29 | 2018-04-03 13:53:29 | NULL       |
|  2 |            3 | 2018-04-04 01:48:07 | 2018-04-04 01:48:07 | 2018-04-03 14:00:00 | 2018-04-03 15:23:52 | NULL       |
|  3 |            3 | 2018-04-04 01:48:07 | 2018-04-04 01:48:07 | 2018-04-03 14:00:30 | 2018-04-03 15:23:52 | NULL       |
|  4 |            3 | 2018-04-04 03:25:07 | NULL                | 2018-04-03 15:25:43 | 2018-04-03 15:25:43 | NULL       |
|  5 |            3 | 2018-04-05 01:25:07 | 2018-04-05 03:48:07 | 2018-04-03 15:26:01 | 2018-04-03 16:06:15 | NULL       |
|  6 |            3 | 2018-04-05 01:25:07 | NULL                | 2018-04-03 16:06:53 | 2018-04-03 16:06:53 | NULL       |
|  7 |            3 | 2018-04-05 03:25:07 | NULL                | 2018-04-03 16:07:22 | 2018-04-03 16:07:22 | NULL       |
+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+

现在的情况是我想要 max(check_in)idorg_entry_id 3 即表中的 72018-04-05 03:25:07

是否还有更多可能的解决方案。

最佳答案

如果每个 org_entry_id 中的 check_in 值是唯一的,或者您不关心每个 org_entry_id 可能有多行:

select
    b.*
from
    (select org_entry_id , max(check_in) check_in from time_sheets group by org_entry_id) a
    join time_sheets b on (a.org_entry_id=b.org_entry_id and a.check_in=b.check_in)

如果 check_in 值在每个 org_entry_id 中不唯一,但您仍希望每个 org_entry_id 占一行:

select
    d.*
from
    (select
        max(b.id) id
      from
        (select org_entry_id , max(check_in) check_in from time_sheets group by org_entry_id) a
        join time_sheets b on (a.org_entry_id=b.org_entry_id and a.check_in=b.check_in)
      group by 
        a.org_entry_id
    ) c
    join time_sheets d on c.id=d.id

如果您按特定 org_entry_id 进行过滤,而 check_in 没有唯一值,但您仍然希望只返回一行:

select
    *
from
    time_sheets
where
    org_entry_id=123
order by
    check_in desc,
    id
limit 1

关于mysql - 如何在mysql中选择除group by之外的其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49627853/

相关文章:

php - 数组中的重复区域

php - 从日期时间字段(mysql)中仅提取日期并将其分配给 php 变量

mysql - cakephp 将自定义查询转换为 cakephp 查询

sql - 外键困惑

mysql - 获取最新的可接受差距的日期

mysql - Laravel 中某个字段最大的 eloquent 的唯一记录?

数据库行快照/修订

mysql - SQL 查询根据正在连接的主表的 PK 填充表

mysql - GROUP BY 和 mysql 5.7

比较日期列和日期时间/时间字符串时的Mysql隐式转换