Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting mysql result from the last 30 days [duplicate]

Tags:

php

mysql

Possible Duplicate:
Get from database but only last 30 days

Hi I have some php code which I use to count the rows in a database from the last 30 days. The problem is, that if I change the piece of code so that the number changes from -30 to -20, the output number goes from 272 to 360 instead of going down.

Here is the code:

$result = mysql_query("SELECT * FROM all_count WHERE DATEDIFF(date,NOW()) = -30 and member ='000002'");
$num_rows60 = mysql_num_rows($result);
like image 533
Sam Williams Avatar asked Dec 21 '12 21:12

Sam Williams


People also ask

How do I get last 30 days records in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How do I get previous month records in MySQL?

select * from orders where order_date>now() - interval 1 month; In the above query, we select rows after past 1 month interval. We use INTERVAL clause and NOW() function to obtain the date 1 month in the past, from present date.

How can I get records between two dates in MySQL?

select *from yourTableName where yourColumnName between 'yourStartingDate' and curdate().

How do I get the latest 2 records in SQL?

To select last two rows, use ORDER BY DESC LIMIT 2.


1 Answers

Try this

select * from `table` where `yourfield` >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

For days, year see below for example.

DATE_SUB(CURDATE(), INTERVAL 15 DAY) /*For getting record specific days*/

DATE_SUB(CURDATE(), INTERVAL 1 YEAR) /*for getting records specific years*/


For Anand, query
BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE() ,INTERVAL 3 MONTH ) 
/* For Getting records between last 6 month to last 3 month
like image 159
softsdev Avatar answered Oct 21 '22 15:10

softsdev