0

以下のクエリの結果を優先度順に並べて表示する必要があります。

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> 

このクエリの結果を優先順位で並べ替えるにはどうすればよいですか。そのため、DateAdded に関係なく、優先度 7 の行が一番上に表示されます。クエリで DateAdded による並べ替えが必要であり、時間の上位 12 行を選択し、それらを優先順位に従って並べ替えます。これはどのように可能ですか?

4

2 に答える 2

0

次のクエリを使用して、最初に null 以外で並べ替えます (追加日の後):

SELECT CouponId, CouponCode, DateAdded, Priority 
FROM Coupon WHERE (IsFeatured = 1) and (IsApproved = 1) 
ORDER BY   DateAdded desc, Priority IS Null, Priority desc limit 12;
于 2013-09-23T09:28:31.853 に答える