mysql按照时间显示出来,MySQL查询按照降序显示当前日期的记录,其他的显示记录...

I am using the following query in my database,

SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends

FROM tbl_sales b WHERE b.active=1

UNION

SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends

FROM tbl_sales b INNER JOIN tb_category c ON b.category_id=c.cat_id

WHERE c.cat_keyword LIKE 'a' ORDER BY sale_ends DESC

and getting the result as follows,

sales_id | category_id |sale_starts | sale_ends

----------|---------------------|------------|--------------

1 | 10 | 2012-03-31 | 2012-04-30

2 | 11 | 2012-03-22 | 2012-04-27

3 | 25 | 2012-03-31 | 2012-04-25

4 | 12 | 2012-04-05 | 2012-04-11

Now i need to get the result as follows, ie the row which has today's date/current date assale_ends must be shown in the top of the order (assuming today's date/current date is 2012-04-11), like shown below-

sales_id | category_id |sale_starts | sale_ends

----------|---------------------|------------|--------------

4 | 12 | 2012-04-05 | 2012-04-11 (today's date)

1 | 10 | 2012-03-31 | 2012-04-30

2 | 11 | 2012-03-22 | 2012-04-27

3 | 25 | 2012-03-31 | 2012-04-25

Need help in this, thanks in advance.

解决方案

Try this ORDER BY clause with condition -

ORDER BY IF(sale_ends = DATE(NOW()), 0, 1), sale_ends DESC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值