Question

I have to show the result of the query below , sorted by the priority.

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)

I though another order by Priority desc would do it but it produces the same result

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> 

How can I order the result of this query by Priority. So that the row with Priority 7 appears at top irrespective of its DateAdded. I need the sort by DateAdded in the query to just pick the top 12 rows w.r.t time and then sort them according to the priority . How is this possible ?

Was it helpful?

Solution

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

OTHER TIPS

Use this query to order by non nulls first (after Date Added):

SELECT CouponId, CouponCode, DateAdded, Priority 
FROM Coupon WHERE (IsFeatured = 1) and (IsApproved = 1) 
ORDER BY   DateAdded desc, Priority IS Null, Priority desc limit 12;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top