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

时间:2018-04-03 10:31:42

标签: 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

我尝试了这样的查询,但会出现full groupBy mode错误

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

所以我已经阅读了手册,我发现它已写入ANY_VALUE但是我不想要的解决方案。我想要在当前id前面的完全正确的max(check_in)

这是一些示例数据

+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+
| 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)id org_entry_id 3 表格中的72018-04-05 03:25:07

可以为此提供更多可能的解决方案。

3 个答案:

答案 0 :(得分:0)

如果check_in值在每个org_entry_id中都是唯一的,或者您并不关心每个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

答案 1 :(得分:0)

试试这个:

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

您应该对select子句中的所有属性使用聚合函数,但不能在group by子句中使用。

答案 2 :(得分:0)

试试这个

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