简体   繁体   English

在数据库中按分组日期排序

[英]ordering by grouped dates in database

hi got a similar problem to this question 嗨,有一个与此问题类似的问题

Tricky "grouped" ordering in SQL SQL中棘手的“分组”排序

but cant seem to make it work with dates 但似乎无法使其与日期一起使用

my table sort of 我的桌子有点

Car_ID   Car_Brand   Car_Model   Car_launch_date
   1     Ford        Fiesta      12-12-2011
   2     Ford        Mustang     09-04-2008
   3     Ford        Focus       10-02-2012
   4     Honda       Civic       11-05-2012
   6     Honda       Jazz        02-05-2011
   7     Toyota      Prius       11-10-2011

i want my results ordered by future dates closest to now, then followed by past dates closest to now (and grouped by brand) 我希望我的结果按最接近现在的将来日期排序,然后再跟最接近现在的过去日期排序(并按品牌分组)

I want my output to be this: 我希望我的输出是这样的:

Car_ID   Car_Brand   Car_Model   Car_launch_date
   7     Toyota      Prius       11-10-2011
   1     Ford        Fiesta      12-12-2011
   3     Ford        Focus       10-02-2012
   2     Ford        Mustang     09-04-2008
   4     Honda       Civic       11-05-2012
   6     Honda       Jazz        02-05-2011

Are your dates in the database as DATE objects or strings? 您在数据库中的DATEDATE对象还是字符串? Because if you try to order them as strings, that won't work! 因为如果您尝试将它们作为字符串订购,那将行不通!

If the dates are DATE s, try: 如果日期是DATE ,请尝试:

SELECT * from my_table
ORDER BY Car_Brand DESC, 
         Car_launch_date < NOW(), 
         ABS(DATEDIFF(Car_launch_date, NOW()))

If they're strings, try: 如果它们是字符串,请尝试:

SELECT * from my_table
ORDER BY Car_Brand DESC, 
         DATE(Car_launch_date) < NOW(), 
         ABS(DATEDIFF(DATE(Car_launch_date), NOW()))

When you specify more than one column in an order by, it orders by the first column, and then by the next if the values in the first column are the same. 当您在一个排序依据中指定多个列时,如果第一列中的值相同,则它将按第一列排序,然后按第二列排序。

This orders by - Car Brand, then - Whether the date is in the past. 此订单的排序方式-汽车品牌,然后-日期是否过去。 Falses are ordered first. 虚假首先被排序。 (So future first), then - Absolute difference in dates (smallest number first) (因此,未来优先),然后-日期的绝对差(最小的数字优先)

Note: I put Car_Brand DESC (reverse alpha order) because that's how you had it above. 注意:我放Car_Brand DESC (反向字母顺序),因为这就是上面的方法。

Thanks to the OP for spotting that you wanted a more complicated order. 感谢OP的发现,您想要更复杂的订单。 But I'd argue this way is better : ) 但我认为这样更好:)

Using previous post as a supplement, the better answer would be: 使用以前的帖子作为补充,更好的答案是:

GROUP BY Car_Brand 
ORDER BY 
CASE WHEN Car_launch_date >= NOW() THEN 0 ELSE 1 END, 
CASE WHEN Car_launch_date >= NOW() THEN DATEDIFF(Car_launch_date, NOW()) ELSE DATEDIFF(NOW(), Car_launch_date) END

The query uses conditionnal statements to determine first if it's a future or past date. 该查询使用条件语句来首先确定它是将来的日期还是过去的日期。 If it's future, it outputs 0 and past it outputs 1 so the future dates will sort first and past dates sort second. 如果是将来,则输出0,过去则输出1,因此将来的日期将排在第一位,过去的日期将排在第二位。 Next another case statement to detect how to compute the difference of days between now and the launch date. 接下来是另一个case语句,用于检测如何计算现在和启动日期之间的天数差。

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

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