简体   繁体   English

仅从 MySQL 中的日期时间 (YYYY-MM-DD HH:MM:SS) 中选择不同的日期

[英]Selecting a distinct date by day only from datetime (YYYY-MM-DD HH:MM:SS) in MySQL

Executing this command brings me the following (all the dates from all columns, so it essentially did the same thing as SELECT date without distinct):执行这个命令给我带来了以下内容(所有列的所有日期,所以它本质上与 SELECT date 没有区别)做了同样的事情:

SELECT DISTINCT date FROM daily ORDER BY date DESC

2013-02-12 16:40:52
2013-02-06 11:48:49
2013-02-06 11:36:41
2013-02-06 11:35:59
2013-02-04 19:38:12
2013-02-04 18:12:30
2013-02-04 09:58:41
2013-02-04 09:43:01
2013-02-04 09:35:51
2013-02-04 09:30:22
2013-02-04 09:24:57
2013-02-04 09:21:09
2013-02-04 08:50:13

What I need:我需要的:

2013-02-12
2013-02-06
2013-02-04
  1. Is there any way to alter my date table and convert it to YYYY-MM-DD instead?有什么方法可以更改我的日期表并将其转换为 YYYY-MM-DD 吗?

  2. If not, is there a way to select distinct dates based only on the day?如果没有,有没有办法仅根据当天选择不同的日期?

mysql> select DATE_FORMAT(Current_Timestamp, '%c %d %Y') from dual;
+--------------------------------------------+
| DATE_FORMAT(Current_Timestamp, '%c %d %Y') |
+--------------------------------------------+
| 2 12 2013                                  |
+--------------------------------------------+
1 row in set (0.01 sec)

mysql> 

of course you would be using your 'daily' table.当然,您将使用“每日”表。

mysql> select DATE_FORMAT(Date, '%c %d %Y') from daily;

or maybe you want或者你想要

mysql> select * from daily group by DATE_FORMAT(Date, '%c %d %Y');

Try this one:试试这个:

SELECT DISTINCT(CONVERT(VARCHAR, CONVERT(DATETIME,[DATE]),23)) AS DT  
FROM DAILY
ORDER BY DT ASC

我发现在大于 N 百万的大型数据集上,按 DATE_FORMAT 分组可能比使用慢 10-20%;

 GROUP BY someid,year(date),month(date),day(date)

So I have just added a small part to sgeddes answer which was close to working for me but I had to as the : AS date_change so I can list the values.所以我刚刚在 sgeddes 答案中添加了一小部分,这对我来说很接近,但我不得不作为:AS date_change 以便我可以列出这些值。

$sql = "SELECT DISTINCT DATE_FORMAT(yourdate, '%Y-%m-%d') as date_change FROM 
daily ORDER BY (yourdate) DESC LIMIT 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

 // output data of each row
 while($row = $result->fetch_assoc()) {
 $test = $row['date_change'];
    echo $test.'<br>';



  }

} else {
echo "0 results";
}

Its very simple use DATE() function.它非常简单地使用 DATE() 函数。

Example:例子:

SELECT DISTINCT DATE(date)
    FROM daily
    ORDER BY date
    DESC

For MySQL, you can use DATE(date) to return just the date.对于 MySQL,您可以使用DATE(date)仅返回日期。 If you want to format it, use DATE_FORMAT :如果要格式化,请使用DATE_FORMAT

SELECT DISTINCT DATE_FORMAT(yourdate, '%Y-%m-%d') 
FROM daily ORDER BY DATE(yourdate) DESC

You cannot change the way the database stores these values.您无法更改数据库存储这些值的方式。

And here is the fiddle: http://sqlfiddle.com/#!2/f50527/6这是小提琴: http ://sqlfiddle.com/#!2/ f50527/6

In Access DB在访问数据库中

SELECT DISTINCT Format(date, 'Short Date')
  FROM daily
 ORDER BY Format(date, 'Short Date') DESC;

暂无
暂无

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

相关问题 将YYYY-MM-DD HH:MM:SS格式的java.util.Date转换为与MySQL DATETIME兼容 - Convert java.util.Date in YYYY-MM-DD HH:MM:SS format to compatible with MySQL DATETIME 在MySQL中将间隔格式设置为&#39;yyyy-mm-dd hh:MM:ss&#39; - Formatting an interval as 'yyyy-mm-dd hh:MM:ss' in MySQL MySQL:将“ yyyy-mm-ddThh-mm-ss.sssZ”转换为“ yyyy-mm-dd hh-mm-ss” - mySQL: convert from “yyyy-mm-ddThh-mm-ss.sssZ” to “yyyy-mm-dd hh-mm-ss” 将MYSQL日期时间值转换为UTC时区格式yyyy-MM-dd&#39;T&#39;HH:mm:ss&#39;Z&#39; - convert MYSQL datetime value to UTC Timezone format yyyy-MM-dd'T'HH:mm:ss'Z' YYYY-MM-DD HH:MM:SS[.fraction] 格式的 MySQL 更新日期字段 - MySQL update date field in YYYY-MM-DD HH:MM:SS[.fraction] format 如何在MySQL中导入Excel存储的数据并保持日期格式为yyyy-mm-dd hh:mm:ss - How to import excel stored data in MySQL and keep date format as yyyy-mm-dd hh:mm:ss 将mysql中的日期字符串从dd / mm / yyyy hh:mm:ss转换为DD MMM YYYY - convert date string sored in mysql from dd/mm/yyyy hh:mm:ss to DD MMM YYYY MM-YYYY 到 YYYY-MM-DD hh:mm:ss 在 php 和 mysql - MM-YYYY to YYYY-MM-DD hh:mm:ss on php and mysql 如何从时间yyyy-mm-dd减去hh:mm时间戳hh:mm:ss时间戳 - How to Minus hh:mm time stamp from the Time yyyy-mm-dd hh:mm:ss timestamp 如何从 YYYY-MM-DD HH:MI:SS 转换 DATETIME。 使用 SQL 到 YYYY-MM - How to convert DATETIME from YYYY-MM-DD HH:MI:SS. to YYYY-MM using SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM