I am actually having a database just like this
+--------------+------------------+------+-----+-------------------+-----------------------------+
| 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 | |
+--------------+------------------+------+-----+-------------------+-----------------------------+
Now I want to get id field and max(check_in) from this table and that too groupBy org_entry_id
I have tried query like this but it will give full groupBy mode
error
select id, max(check_in) from time_sheets group by org_entry_id;
So I have read the manual and I found that it was written to ANY_VALUE
but that solution I don't want . I want the exact correct id
which is in front of the current max(check_in)
.
This is some sample data
+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+
| 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 |
+----+--------------+---------------------+---------------------+---------------------+---------------------+------------+
Now the situation is I want max(check_in)
and id
for org_entry_id
3
that is 7
,2018-04-05 03:25:07
from the table.
Can there be any more possible solution for this .