Tuesday 4 September 2018

MYSQL query for a single data range

I have a simple database with a table called history that stores data in dates.
The table is called history and there have a column name for "user", "data" and "creation" which is the date. Data stamps in "creation" are like this "2013-01-13 07:45:49"
I tried some very basic stuff with PHP and I get data, for example: $result = mysql_query("SELECT data FROM history WHERE user='705'");
Will display all the data from the user "705"
But how would I select the same data for user 705 only in a specific data range? For example only show data from the last week? Or any date range...
"data" by the way are just numbers like "88993" so with my current query I get a long and long list of numbers. I just want to narrow my selection by dates.
Help is appreciated. Thanks

Try dates between..
select * from yourtable
where userid =785
and date between yourdate1 and yourdate2
;

You may use interval to specify the dates gap that you need:
select * from yourtable
    where userid =785
    and date between '2013-01-13 07:45:49'
    and '2013-01-13 07:45:49' interval -10 day
    ;

Try this based on your last comment:
select * from yourtable
        where userid =785
        and creation between Now()
        and Now() interval -7 day
order by creation
limit 7
;

0 comments:

Post a Comment