2011-04-18 86 views
1

是否可以檢查列中的某些數據? 爲爲例,我有一個名爲列和'added'值是一樣的東西'2011-04-18 10:44:42'檢查列中的某些數據

,並有可能從塔只有日期'2011-04-18'

$date = sqlesc(get_date_time()); 
$res = mysql_query("SELECT id, username FROM users WHERE added = 
     $date ORDER BY username") or print(mysql_error()); 
+0

什麼是「added」列的數據類型? – diEcho 2011-04-18 11:16:07

+0

閱讀:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html – diEcho 2011-04-18 11:18:45

回答

1
SELECT id, username 
FROM users 
WHERE added >= '2011-04-18' 
     AND added < '2011-04-18' + INTERVAL 1 DAY 
+0

我不認爲這是有效的MYSQL DATE_FORMAT()? – wired00 2011-04-18 11:21:38

+1

@ wired00:你覺得沒錯。它不*作爲*效率高,它比'DATE_FORMAT'更有效*,因爲它是可變的(能夠在'added'上使用索引),而'DATE_FORMAT'不是。 – Quassnoi 2011-04-18 11:24:30

+0

是的,它的工作,感謝Quassnoi – 2011-04-18 11:27:15

0

最簡單的解決方案...工程DATETIME檢查和所有的文本類型字段(如VARCHAR,TEXT等)。

$date = sqlesc(get_date_time()); 
$res = mysql_query("SELECT id, username FROM users WHERE added LIKE '$date %' 
        ORDER BY username") or print(mysql_error()); 
0

如果我正確理解你的問題,你需要mysql DATE_FORMAT()它將使您的數據庫日期值格式到YYYY-MM-DD

使用這樣的格式:

$res = mysql_query("SELECT id, username FROM users WHERE DATE_FORMAT(added, '%Y-%c-%d') = 
     $date ORDER BY username") or print(mysql_error()); 

希望幫助