Home Backend Development PHP Tutorial 怎么实现这样的排序

怎么实现这样的排序

Jun 13, 2016 pm 01:40 PM
on sale

如何实现这样的排序
在商品列表中 新添加的商品并且是上架状态的有有库存的默认排在前面,下架的库存少的或者为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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Deal | Affordable HP Victus gaming laptop with RTX 3050-beating RX 6550M gets 40% discount in Best Buy sale Deal | Affordable HP Victus gaming laptop with RTX 3050-beating RX 6550M gets 40% discount in Best Buy sale Aug 09, 2024 pm 09:51 PM

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 $

Deal | 2024 Asus ROG Zephyrus G16 with Intel Core Ultra 9 and Nvidia RTX 4070 nears all-time low Deal | 2024 Asus ROG Zephyrus G16 with Intel Core Ultra 9 and Nvidia RTX 4070 nears all-time low Aug 01, 2024 am 10:56 AM

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

Deal | Nvidia Shield TV Pro with identical hardware to Nintendo Switch drops to record-low price Deal | Nvidia Shield TV Pro with identical hardware to Nintendo Switch drops to record-low price Aug 11, 2024 am 06:43 AM

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 Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content Warning Steam Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content Warning Jun 26, 2024 pm 03:40 PM

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

Deal | E-bike sale slashes $500 off 25 mph Velotric Packer 1 cargo bike with free US shipping Deal | E-bike sale slashes $500 off 25 mph Velotric Packer 1 cargo bike with free US shipping Aug 01, 2024 pm 05:04 PM

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

Deal | Samsung 32-inch 240 Hz 4K Odyssey Neo G8 gaming monitor gets massive 43% discount in Amazon deal Deal | Samsung 32-inch 240 Hz 4K Odyssey Neo G8 gaming monitor gets massive 43% discount in Amazon deal Aug 07, 2024 pm 08:52 PM

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

Deal | Google Pixel 8 Pro sees a massive 38% discount, dropping to a new record-low price Deal | Google Pixel 8 Pro sees a massive 38% discount, dropping to a new record-low price Sep 11, 2024 am 06:41 AM

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

What is the difference between on, in, as, and where in Mysql? What is the difference between on, in, as, and where in Mysql? Jun 03, 2023 am 11:37 AM

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

See all articles