mysql获取最近30天,从最近30天获取mysql结果

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);

解决方案

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

你可能感兴趣的:(mysql获取最近30天)