怎么实现这样的排序
如何实现这样的排序
在商品列表中 新添加的商品并且是上架状态的有有库存的默认排在前面,下架的库存少的或者为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 管理器中测试好各种情况再书写代码

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The HP Victus 15 is 1 15.6-inch entry-level gaming laptop that ordinarily wouldn't be worth much consideration, however a new Best Buy deal offers 40% off the entry-level gaming laptop, bringing the price down from $799.99 to a very budget-friendly $

The newly-released 2024 Asus ROG Zephyrus G16 is on sale, and you can pick up the variant with Intel Core Ultra 9 185H and Nvidia RTX 4070 at $1,649.99 on Best Buy. This is a decent $350 price cut from what the gaming laptop generally goes for, and t

Nvidia introduced the Shield TV Pro back in 2019, and it's yet to see a successor. It features hardware identical to that of the Nintendo Switch, which has also been in the market for a long time without a successor. Now, even though this Android TV

Steam's Summer Sale has previously played host to some of the best game discounts, and this year seems to be stacking up for another home run by Valve. A trailer (watch below) teasing some of the Steam Summer Sale discounted games was just released i

Electric cargo bikes are often touted as car replacements because of their ability to carry large, heavy items, including children, with ease — especially for those with shorter daily commutes. Unfortunately, they typically also come with car-like pr

Samsung's Odyssey gaming monitor line-up features some of the best gaming monitors money can buy, often delivering on top-tier features and specifications along with their flagship pricing. The Odyssey Neo G8 sits at the higher end of Samsung's Odyss

Google Pixel 9 series became official last month. Although the new Android phones have seen upgrades in multiple areas, the last-gen lineup is still worth considering if you want a Pixel. The good part is that the top-end model from the previous seri

The difference between Mysqlon, in, as, and where Answer: Where query conditions, use on for internal and external connections, as as an alias, in to query whether a certain value creates 2 tables in a certain condition: student, scorestudent: score: whereSELECT*FROMstudentWHEREs_sex=' Male'For example: onSELECT*FROMstudentLEFTJOINscoreonstudent.s_id=score.s_id; combination of on and where: SELECT*FROMstudentLEFTJOINs
