简体   繁体   English

MySQL选择最近4小时内的记录

[英]MySQL Select Record Within Last 4 Hours

I have a last_post_time field set up as DATETIME in my user database. 我在用户数据库中将last_post_time字段设置为DATETIME。 I want to check if the current user has a successful post made in the last 4 hours in my PHP file, and if not, do something. 我想检查当前用户在最近4小时内是否在我的PHP文件中发布了成功的帖子,如果没有,请执行某些操作。 Here is what I have: 这是我所拥有的:

$query = mysqli_query($con,"SELECT last_post_time FROM xxyyzz_users WHERE ID=$user_ID");
$row = mysqli_fetch_array($query);
$userpost = $row[last_post_time];

if ($userpost == '') { // no post, do this } else { // has a post, do this }

1) How do I add the post time check to my query 1)如何将发布时间检查添加到查询中

2) Is my PHP correct? 2)我的PHP是否正确?

You can select only those users directly in your query and return the number of users meeting that criteria 您可以直接在查询中仅选择那些用户,然后返回满足该条件的用户数

SELECT count(*) as u_count
FROM xxyyzz_users 
WHERE ID=$user_ID
and now() - interval 4 hour <= last_post_time 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM