简体   繁体   中英

Selecting from DATETIME field?

Currently, I am doing mySQL queries as such selecting a unix time stamp from a database, like so:

SELECT DATE(FROM_UNIXTIME(time)) AS theday, COUNT(id) AS thecount FROM `table`
WHERE `time`>=UNIX_TIMESTAMP(CURRENT_DATE - > INTERVAL 14 DAY) 
GROUP BY theday 
ORDER by theday DESC

How would I go about doing a similar query but selecting a DATETIME format? Instead of a unix time stamp?

For example,

0000-00-00 00:00:00

我认为MySql DATE_FORMAT将为您工作。

mysql_query("SELECT DATE_FORMAT(time, '%Y-%m-%d %H:%i:%s') AS theday, COUNT(id) AS thecount FROM table WHERE time>=UNIX_TIMESTAMP(CURRENT_DATE - INTERVAL 14 DAY) GROUP BY theday ORDER by theday DESC");

Update:


Based on your comment, you can use DATE function to select date portion from your datetime field:

SELECT DATE(time)

How would I go about doing a similar query but selecting a DATETIME format? Instead of a unix time stamp?

Instead of:

SELECT DATE(FROM_UNIXTIME(time))

You can select your datetime field directly:

SELECT time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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