gpt4 book ai didi

mysql - 按优先级排序的两列排序

转载 作者:行者123 更新时间:2023-11-29 13:34:18 25 4
gpt4 key购买 nike

我必须显示下面按优先级排序的查询结果。

mysql> select CouponId, CouponCode, DateAdded, Priority from Coupon where IsFeatured=1 and IsApproved=1 order by DateAdded desc limit 12;
+----------+--------------+---------------------+----------+
| CouponId | CouponCode | DateAdded | Priority |
+----------+--------------+---------------------+----------+
| 42699 | cc2 | 2013-09-12 14:54:39 | NULL |
| 42698 | c1 | 2013-09-12 14:53:36 | NULL |
| 42697 | cc2 | 2013-09-12 14:51:57 | NULL |
| 42679 | GLMR20 | 2013-05-14 13:21:07 | 5 |
| 42678 | HKBAJAJ20 | 2013-05-14 12:35:31 | 1 |
| 42677 | SIPPER51 | 2013-05-14 12:11:36 | NULL |
| 42654 | GL13MAYCHILL | 2013-05-14 07:09:08 | 7 |
| 41978 | POLARSPL | 2013-05-03 13:31:32 | NULL |
| 41958 | COMBO30 | 2013-05-03 12:16:53 | NULL |
| 41357 | BRANDED60 | 2013-04-25 13:49:56 | NULL |
| 41073 | PKCCR500 | 2013-04-22 11:10:27 | NULL |
| 40794 | SWC15 | 2013-04-18 13:57:54 | NULL |
+----------+--------------+---------------------+----------+
12 rows in set (0.00 sec)

我虽然按 Priority desc 的另一个订单可以做到这一点,但它会产生相同的结果

mysql> select CouponId, CouponCode, DateAdded, Priority from Coupon where IsFeatured=1 and IsApproved=1 order by DateAdded desc, Priority desc limit 12;
+----------+--------------+---------------------+----------+
| CouponId | CouponCode | DateAdded | Priority |
+----------+--------------+---------------------+----------+
| 42699 | cc2 | 2013-09-12 14:54:39 | NULL |
| 42698 | c1 | 2013-09-12 14:53:36 | NULL |
| 42697 | cc2 | 2013-09-12 14:51:57 | NULL |
| 42679 | GLMR20 | 2013-05-14 13:21:07 | 5 |
| 42678 | HKBAJAJ20 | 2013-05-14 12:35:31 | 1 |
| 42677 | SIPPER51 | 2013-05-14 12:11:36 | NULL |
| 42654 | GL13MAYCHILL | 2013-05-14 07:09:08 | 7 |
| 41978 | POLARSPL | 2013-05-03 13:31:32 | NULL |
| 41958 | COMBO30 | 2013-05-03 12:16:53 | NULL |
| 41357 | BRANDED60 | 2013-04-25 13:49:56 | NULL |
| 41073 | PKCCR500 | 2013-04-22 11:10:27 | NULL |
| 40794 | SWC15 | 2013-04-18 13:57:54 | NULL |
+----------+--------------+---------------------+----------+
12 rows in set (0.00 sec)

mysql>

如何按优先级排序此查询的结果,以便优先级为 7 的行显示在顶部,而不管其添加日期如何?

我需要在查询中按 DateAdded 排序,以仅选择时间方面的前 12 行,然后根据优先级对它们进行排序。

最佳答案

select CouponId, CouponCode, DateAdded, Priority 
from
(
select CouponId, CouponCode, DateAdded, Priority
from Coupon
where IsFeatured=1
and IsApproved=1
order by DateAdded desc
limit 12
) x
order by Priority desc

关于mysql - 按优先级排序的两列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18955739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com