gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:48:14 24 4
gpt4 key购买 nike

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

+--------------+------------------+------+-----+-------------------+-----------------------------+
| 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com