0

Im new to mysql coding so help would be appreciated.

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

2 Answers 2

2

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
;

Reference: you may check all date time functions here.

Sign up to request clarification or add additional context in comments.

4 Comments

@nibb11 try this one out and comment please. :)
Thanks for the reply but this does not work: $result = mysql_query("select data from history where user ='705' and date between '2013-01-13 08:02:5'2 and '2013-01-13 08:00:51'");
Does MYSQL does not have just a number query? Like * shows all, would it not be easier to pull for example only the last 7 records, this would be a week, like pull 7 instead of everything. Of course dates would be better but I don´t seem to get it to work.
@nibb11 so you mean you only have one record per day for this user? So from today till 7 days back if we get records for this user will that be alright or if you have multiple records per day (with different times) do you want to limit it by the latest 7 records?
0

For last week try this:

select data from history
where (user='705') AND (date between date_sub(now(),INTERVAL 1 WEEK) and now());

you can also substitute "1 WEEK" with "1 MONTH", "2 MONTH" ,"1 YEAR", and ...

2 Comments

Sorry but this codes return nothing, I also don´t understand why you are closing the query in this example with ; if its not even in bracked like here: w3schools.com/php/php_mysql_select.asp The codes on this tutorial, all work for me fine which means they are valid in PHP.
I edited my answer, try this. as said in this Doc dev.mysql.com/doc/refman/5.0/en/entering-queries.html you can ending your query with semi-colon...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.