![](/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.