简体   繁体   English

由于mysql中的Clause命令,我的查询将无法正常工作

[英]my query won't work due to order by Clause in mysql

I am writing query for displaying data and added order by clause in that query. 我正在编写查询以显示数据并在该查询中添加order by子句。 My query Is 我的查询是

SELECT * FROM `coupons` 
WHERE `status` = 'A' &&  `type` = '2'    
&&  `time` > CURDATE() && `start_date` <= CURDATE()
order by `stocks` asc 

In above query it get sorted by 'stocks' but in some cases stocks is zero , that case i want show this row at the last. 在上面的查询中,它按“股票”排序,但在某些情况下股票为零,这种情况我想在最后显示这一行。 what should i do please help . 我该怎么办请帮助。 thanks in advance. 提前致谢。

order by CASE WHEN `stocks` = 0 THEN 1 ELSE 0 END asc , `stocks` asc 
SELECT *, (if(`stocks` != 0,`stocks`,"")) as newstocks FROM `coupons` 
WHERE `status` = 'A' &&  `type` = '2'    
&&  `time` > CURDATE() && `start_date` <= CURDATE()
order by newstocks asc 

Use above code. 使用上面的代码。

By default it is asc so you don't have to mention it. 默认情况下它是asc所以你不必提及它。 Try the below one 试试下面的一个

SELECT * FROM coupons WHERE status = 'A' && type = '2' && time > CURDATE() && start_date <= CURDATE() order by case stocks != 0 then stocks end, stocks SELECT * FROM优惠券WHERE status ='A'&& type ='2'&& time> CURDATE()&& start_date <= CURDATE()按个案股票排序!= 0然后股票结束,股票

It will first list the items with stocks is nozero and then list the rows with stocks=zero 它将首先列出库存为nozero的项目,然后列出库存=零的行

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

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