[英]MySQL: Getting only one row for multiple rows with the same date?
I have a MySQL query and it returns several rows with a date in one column, thats my query: 我有一个MySQL查询,它返回几行,其中一列带有日期,这就是我的查询:
select * from (big subselect) sub
I get 6 rows for example and one column in those 6 rows has a date, the column is called my_date. 例如,我得到6行,而这6行中的一列有一个日期,该列称为my_date。 Is there a way to get only those rows that have a date that is not the same day?
有没有办法只获取那些日期不是同一天的行? Example:
例:
29.10.2012 01:51:10
29.10.2012 01:51:10
29.10.2012 01:51:10
29.10.2012 02:43:18
29.10.2012 02:43:18
29.10.2012 02:43:18
--> I want only one of the rows, because all 6 are the same day. ->我只需要其中一行,因为所有6行都是同一天。
29.10.2012 01:51:10
30.10.2012 01:51:10 --> different
29.10.2012 01:51:10
29.10.2012 02:43:18
29.10.2012 02:43:18
29.10.2012 02:43:18
--> I want 2 rows, because there are 2 different days ->我要2行,因为有2天不同
The my_date column is not the only one in each row, but I want it as criteria. my_date列不是每一行中唯一的一列,但我希望将其作为条件。 How could I do that?
我该怎么办?
Thanks! 谢谢!
You will need to select only these MAX(my_date)
, but you will need to group by some other filed. 您只需要选择这些
MAX(my_date)
,但是您需要按其他MAX(my_date)
分组。 Something like this: 像这样:
select s1.*
from sub as s1
INNER JOIN
(
SELECT MAX(my_date) AS MaxDate, SomeId
FROM sub
GROUP BY someId
) AS s2 ON s1.my_date = MaxDate AND s1.SomeId = s2.SomeId;
You can group only dates like this: 您可以像这样仅将日期分组:
SELECT * FROM sub
GROUP BY DATE_FORMAT(my_date,'%Y-%m-%d')
Result: 结果:
| ID | MY_DATE |
---------------------------------------
| 1 | October, 29 2012 01:51:10+0000 |
| 2 | October, 30 2012 01:51:10+0000 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.