如何 select mysql 中分组依据以外的其他列
How to select other columns other than group by in mysql
我实际上有一个这样的数据库
+--------------+------------------+------+-----+-------------------+-----------------------------+
| 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 | |
+--------------+------------------+------+-----+-------------------+-----------------------------+
现在我想从这个 table 和那个 groupBy org_entry_id
中获取 id 字段和 max(check_in)
我试过这样的查询,但它会给出 full groupBy mode
错误
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)
和 id
用于 org_entry_id
3
即 7
,2018-04-05 03:25:07
来自 table.
是否还有更多可能的解决方案。
如果 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
没有唯一值,但您仍然希望 return 中只有一行:
select
*
from
time_sheets
where
org_entry_id=123
order by
check_in desc,
id
limit 1
试试这个:
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 子句中的所有属性使用聚合函数。
试试这个
select max(id),org_entry_id, max(check_in) from time_sheets group by org_entry_id;
我实际上有一个这样的数据库
+--------------+------------------+------+-----+-------------------+-----------------------------+
| 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 | |
+--------------+------------------+------+-----+-------------------+-----------------------------+
现在我想从这个 table 和那个 groupBy org_entry_id
中获取 id 字段和 max(check_in)我试过这样的查询,但它会给出 full groupBy mode
错误
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)
和 id
用于 org_entry_id
3
即 7
,2018-04-05 03:25:07
来自 table.
是否还有更多可能的解决方案。
如果 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
没有唯一值,但您仍然希望 return 中只有一行:
select
*
from
time_sheets
where
org_entry_id=123
order by
check_in desc,
id
limit 1
试试这个:
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 子句中的所有属性使用聚合函数。
试试这个
select max(id),org_entry_id, max(check_in) from time_sheets group by org_entry_id;