![](/img/trans.png)
[英]How to query data from a table where **created_at** date = **today's date** ?
[英]How to display table data by today's date?
我有一个名为gamechedules的数据库和一个名为dec1212的表。 这里面表是列date (YYYY-MM-DD)
division
, field
, time
, team1
和team2
的顺序。
我已经在页面上显示了数据,但我需要将其限制为仅显示数据库中日期等于当前日期的行。 因此,如果有10行,而其中只有5行在date列中有今天的日期,则仅应显示这些行。 这是我当前的查询代码,我知道这是错误的,但是如果有人可以帮助我更正它,那就太好了:
当前代码:
//specifies that it is getting data from the table and limited num rows
$result = mysql_query("SELECT * FROM `dec1212` LIMIT 0, 10 ") or die(mysql_error());
这是我尝试通过日期限制数据的方法:
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate()") or die(mysql_error());
我知道我的代码不正确,所以这就是为什么我寻求帮助。
试试这个只是信号线查询
$result = mysql_query('SELECT * FROM dec1212 WHERE DATE(CONVERT_TZ(date,"+00:00","-8.00")) = DATE(CONVERT_TZ(UTC_TIMESTAMP(),"+00:00","-8.00"))') or die(mysql_error());**
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
尝试这个
$result = mysql_query("SELECT * FROM `dec1212` WHERE `date` = CURDATE()") or die(mysql_error());
尝试这个
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate") or die(mysql_error());
您正在使用变量而不是函数,所以只需像这样更改
$myDate = date('Y-m-d');
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = '$myDate'") or die(mysql_error());
确保数据库中的日期字段仅包含日期,而不包含时间
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= '$myDate'") or die(mysql_error());
如果您的日期字段包含datetime数据类型,则使用:
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= date('$myDate')") or die(mysql_error());
让数据库完成工作,
SELECT * FROM `dec1212` WHERE DATE(date) = DATE(NOW())
请注意, $myDate
是变量,而不是函数。 因此,您的代码将是:
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
表名或字段名应以克拉号“而不是单引号'
否则您可以省略。
如果要使用MySQL NOW()
,则需要考虑Raj Mohan提到的时区转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.