Michael
Michael

Reputation: 33

How to get last first and last from table with same date (query)?

I want to ask about query, I want to make last insert item system.

If some people insert new item, I will insert date and time to the track table http://i622.photobucket.com/albums/tt306/mchlhlm/query/track_zps9jqrwcbp.jpg

and then, I want to get the first and last rows of the day per date:

http://i622.photobucket.com/albums/tt306/mchlhlm/query/sdsa_zpsntsjvoyz.jpg

What query to get like that data?

Upvotes: 0

Views: 73

Answers (2)

shawnt00
shawnt00

Reputation: 17925

I think the first column is harder in MySQL but I can give you the rest. Do you really need to number the output rows that way?

select
    Employee_ID, `date`,
    min(Insert_date_time) as First_Time, max(Insert_date_time) as Last_Time
from track
group by Employee_ID, `date`

Upvotes: 1

Mureinik
Mureinik

Reputation: 311893

A simple group by clause should do the trick:

SELECT   employee_id, 
         `date`,
         MIN(insert_data_time) AS first_time, 
         MAX(insert_data_time) AS last_time
FROM     track
GROUP BY employee_id, `date`

Upvotes: 1

Related Questions