从数据库获取,但只有最近30天 [英] Get from database but only last 30 days

查看:106
本文介绍了从数据库获取,但只有最近30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个php从我的mysql数据库中获取数据:

  $ wpdb-> get_var(select count(*)from。$ wpdb-> prefix。newsletter where status ='C'); ?> 

你可能会注意到我在wordpress中,所以我使用$ wpdb变量。 >

这将从 status ='C'的表返回所有数据。这是一切正常,但我只需要获取过去30天的数据。我不知道mysql是否存储有关此条目存储的数据,但是在 status 在同一行中创建了另一个名为的列 code>。这将以以下格式存储日期:



2011-10-14 15:33:58



有没有办法使用它来更新我的代码,只是从过去一个月里检索数据?



任何建议这里非常感激。



感谢C

解决方案

有adddate ()函数,你可以使用这个:)

  $ wpdb-> get_var(select count from。$ wpdb->前缀。newsletter where status ='C'and date_format(created,'%Y%m%d')> date_format(adddate(now(),interval -30 day),'% Y%m%d')); 

这一个是30天之后,但您可以用间隔替换间隔-30天 -1个月或访问 dev.mysql 你所有的解释。



希望它有帮助。


I'm using this php to fetch data from my mysql database:

$wpdb->get_var("select count(*) from " . $wpdb->prefix . "newsletter where status='C'"); ?>

You may notice i'm in wordpress so i'm using the $wpdb variable.

This returns all data from the table where status='C'. This is all working fine but I need to only get the data from the past 30 days. I'm not sure if mysql stores data about when this entry was stored but I do have another column called created in the same row as status. This stores the date in the following format:

2011-10-14 15:33:58

Is there any way to use that to update my code to only retreive the data from the past month?

Any advise here is greatly appreciated.

Thanks C

解决方案

There is adddate() function in MySQL wich you can use for this :)

$wpdb->get_var("select count(*) from " . $wpdb->prefix . "newsletter where status='C' and date_format(created,'%Y%m%d')>date_format(adddate(now(),interval -30 day),'%Y%m%d')");

This one is for 30 days back but you can replace the "interval -30 day" with "interval -1 month" or visit dev.mysql where you have all explained.

Hope it helps.

这篇关于从数据库获取,但只有最近30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆