Home  >  Article  >  Backend Development  >  怎么实现这样的排序

怎么实现这样的排序

WBOY
WBOYOriginal
2016-06-13 13:40:17773browse

如何实现这样的排序
在商品列表中 新添加的商品并且是上架状态的有有库存的默认排在前面,下架的库存少的或者为0的排在后面。
goods_id是自增的。大的就是最新添加的。库存字段:is_on_sale 1为上架 0为下架 库存数量:goods_number  

请问sql怎么写

------解决方案--------------------
ORDER BY `is_on_sale` DESC, `goods_id` DESC, `goods_number` DESC
------解决方案--------------------
order by is_on_sale desc,goods_number desc,goods_id desc


order by可以多重使用,优先级以先后顺序排列,
is_on_sale 为1的在上边,下架的在下边
goods_number 在上边的排序结果中 库存多的在前,
goods_id 在以前结果中,同样条件下新发的在最前

------解决方案--------------------
g.is_on_sale desc 表示上架的摆在前面
所以 上架且库存为0 的也会出现在下架的前面
如果你不想出现这种情况,就要加判断条件
g.is_on_sale and g.goods_number>0 desc
以此类推

你应该在 sql 管理器中测试好各种情况再书写代码

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn