Php MySQL从指定的时间段中选择记录
我正在为我的游戏创建一个排行榜,我有下面的PHP函数,它从我的数据库中选择数据,并按分数排序,WHERE语句将其限制为月份Php MySQL从指定的时间段中选择记录,php,mysql,Php,Mysql,我正在为我的游戏创建一个排行榜,我有下面的PHP函数,它从我的数据库中选择数据,并按分数排序,WHERE语句将其限制为月份 public function getTopMonthMobileUsersByPoints() { try { $query = "select u.id ,u.name ,u.email
public function getTopMonthMobileUsersByPoints() {
try {
$query = "select
u.id
,u.name
,u.email
,u.password
,u.facebook_id
,u.created_date
,u.token_uid
,sum(s.total_points) as earned_points
,u.used_points
,u.gender
,u.birthdate
,0 AS ranking
from
mobile_user u
join score s on s.mobile_user_id = u.id
where
year(report_date) = year(now()) and
month(report_date) = month(now()) and
group by
u.id
having
sum(s.total_points) > 0
order by
sum(s.total_points) desc
limit 100 ";
$stmt = $this->db->prepare($query);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, 'class\Model\MobileUser');
return $stmt->fetchAll();
} catch (PDOException $e) {
$this->logger->error("PDO: ".$e->getMessage(), ["class"=>get_class($this),"method"=>__METHOD__]);
return null;
}
}
我想包括一个类似的函数,它的作用基本相同,但mysql从给定的时间范围内选择记录,例如从2018年6月到2018年8月
有没有办法让mysql从未来选择记录?欢迎提供任何指导通常,对于MySQL日期比较,您只需提供一个字符串时间戳:
SELECT * FROM [table] [JOINS] WHERE report_date >= "2018-06-01" AND report_date < "2018-09-01"
从[表][连接]中选择*其中报告日期>=“2018-06-01”和报告日期<“2018-09-01”
您可以将日期作为字符串作为参数传递,只需在查询中使用变量即可。不过别忘了引语;所有字符串都需要用字符串分隔符进行封装。
report\u date>='2018-06-01'和report\u date<'2018-09-01'
应该可以正常工作,因为日期相当于午夜的时间戳。