mysql. slipt date and time and set min as checkin and maximum as checkout

i hv an table in MySQL in which my users attendance store, i want to split it and min time as checkin and max time as checkout (eg, id,date,checkin,checkout)

my table is as …

mysql. slipt date and time and set min as checkin and maximum as checkout

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have to use MIN(), MAX(), DATE(), TIME() and GROUP BY functions

Lets create a sample db

CREATE TABLE `my_table` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `check_in` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB   DEFAULT CHARSET=latin1;

INSERT INTO `my_table` (`id`, `user_id`, `check_in`) VALUES
(1, 3, '2021-07-04 10:18:19'),
(2, 3, '2021-07-04 11:18:19'),
(3, 3, '2021-07-04 13:18:19'),
(4, 3, '2021-07-05 08:18:19'),
(5, 3, '2021-07-05 20:18:19');

And Query is…

SELECT
    user_id,
    DATE(`check_in`) as date,
    TIME(MIN(`check_in`)) AS CheckIn,
    TIME(MAX(`check_in`)) AS Checkout
FROM
    `my_table`
GROUP BY
    user_id,  DATE(`check_in`)

And the result will be

user_iddateCheckInCheckout
32021-07-0410:18:1913:18:19
32021-07-0508:18:1920:18:19

db<>fiddle here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x