Damien-Amen
Damien-Amen

Reputation: 7512

Limiting records number of days and count

Let's say I have a table in MySQL DB with following columns

employee, status, work, start_date

Consider that start_date column is date and time.

If I do

SELECT employee, status, work, start_date from table_name WHERE DATE(date) >= CURDATE()-10

this will give me records from Current date - 10 days. In this case I might get 1 record to 100 records based on the data.

I need only 10 records based on date/time (e.g. if there are 10 employees that started to work today then I should get only today's records and not 10 days record)

How can I do that?

Upvotes: 1

Views: 82

Answers (2)

radar
radar

Reputation: 13425

you need to use order by on start_date and limit

SELECT employee, status, work, start_date 
from table_name 
order by start_date desc
limit 10

Upvotes: 1

Erik
Erik

Reputation: 3636

You mean you want the ten most recent entries? You can add an ORDER BY to set the order in which the results come back, and a LIMIT to reduce the total number of results.

SELECT employee, status, work, start_date from table_name 
WHERE DATE(date) >= CURDATE()-10
ORDER BY date DESC
LIMIT 10

Upvotes: 2

Related Questions