简体   繁体   中英

MySQL - SELECT MIN time per user_id per date

I originally have a table with two fields user_id and log_time both of VAR_CHAR data types. sample input

user_id       log_time
00148   2013-05-25 08:15:03
00176   2013-05-25 09:09:32
00152   2013-05-25 10:25:55
00146   2013-05-25 10:39:14
00148   2013-05-25 21:23:29
00146   2013-05-25 21:24:52
00152   2013-05-25 21:25:15
00176   2013-05-25 21:26:34
00148   2013-05-26 09:25:53
00176   2013-05-26 09:54:09
00152   2013-05-26 09:58:21
00146   2013-05-26 10:19:04
00148   2013-05-26 18:13:26
00146   2013-05-26 18:14:04
00152   2013-05-26 19:55:20
00176   2013-05-26 19:56:51

i can't alter this table so what i did was this query to be able to separate date and time:

SELECT  `user_id` , 
        SUBSTRING(  `log_time` , 1, 10 ) AS  'date',
        SUBSTRING(  `log_time` , 11 ) AS  'time'
FROM    `user_log` 
ORDER   BY  `user_id` 

i had this output:

u

ser_id    date       time
00148   2013-05-25   08:15:03
00176   2013-05-25   09:09:32
00152   2013-05-25   10:25:55
00146   2013-05-25   10:39:14
00148   2013-05-25   21:23:29
00146   2013-05-25   21:24:52
00152   2013-05-25   21:25:15
00176   2013-05-25   21:26:34
00148   2013-05-26   09:25:53
00176   2013-05-26   09:54:09
00152   2013-05-26   09:58:21
00146   2013-05-26   10:19:04
00148   2013-05-26   18:13:26
00146   2013-05-26   18:14:04
00152   2013-05-26   19:55:20
00176   2013-05-26   19:56:51

now what i want to get is the first entry (earliest time) on a specific date of a user *noting that there can be two inputs of user per date

i been digging on ORDER BY, GROUP BY, JOINs, DISTINCT but can't seem to find how i can really work it out..i was able to have the desired output through if conditions on java, but i need it to be on a query. hoping for some help.. by the way i am using mysql on phpmyadmin

You did not group them by date.

SELECT  user_ID, MIN(log_time) log_time
FROM    tableName
GROUP   BY user_ID, DATE(log_time)
ORDER   BY user_ID, log_time

OUTPUT

╔═════════╦═════════════════════╗
║ USER_ID ║      LOG_TIME       ║
╠═════════╬═════════════════════╣
║     146 ║ 2013-05-25 10:39:14 ║
║     146 ║ 2013-05-26 10:19:04 ║
║     148 ║ 2013-05-25 08:15:03 ║
║     148 ║ 2013-05-26 09:25:53 ║
║     152 ║ 2013-05-25 10:25:55 ║
║     152 ║ 2013-05-26 09:58:21 ║
║     176 ║ 2013-05-25 09:09:32 ║
║     176 ║ 2013-05-26 09:54:09 ║
╚═════════╩═════════════════════╝

but if you want to separate DATE and TIME

SELECT  user_id , 
        SUBSTRING(log_time, 1, 10) AS 'date',
        MIN(SUBSTRING(log_time, 11)) AS 'time'
FROM    TableName
GROUP   BY user_id, SUBSTRING(log_time,1,10)
ORDER   BY user_id, log_time

OUTPUT

╔═════════╦════════════╦═══════════╗
║ USER_ID ║    DATE    ║   TIME    ║
╠═════════╬════════════╬═══════════╣
║     146 ║ 2013-05-25 ║  10:39:14 ║
║     146 ║ 2013-05-26 ║  10:19:04 ║
║     148 ║ 2013-05-25 ║  08:15:03 ║
║     148 ║ 2013-05-26 ║  09:25:53 ║
║     152 ║ 2013-05-25 ║  10:25:55 ║
║     152 ║ 2013-05-26 ║  09:58:21 ║
║     176 ║ 2013-05-25 ║  09:09:32 ║
║     176 ║ 2013-05-26 ║  09:54:09 ║
╚═════════╩════════════╩═══════════╝

Soemthing like this. Although it won't be quick using substringed fields like this

SELECT user_id , SUBSTRING( log_time , 1, 10 ) AS `date`, MIN(SUBSTRING( log_time , 11 )) AS `time` 
FROM user_log
GROUP BY user_id , SUBSTRING( log_time , 1, 10 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM