Home Database Mysql Tutorial mysql排序的妙用(类似置顶)_MySQL

mysql排序的妙用(类似置顶)_MySQL

Jun 01, 2016 pm 01:48 PM
merchandise develop

bitsCN.com

 

今天在做一个2次开发的时候,出现一个需求,
需要在商品分类页里面带一个参数,也就是商品ID,
如果分类链接里面有这个ID的时候就需要把这个商品排在分类商品列表的第1个,
原来的思路是,选择分类后,在PHP里面修改,把那个商品修改到第一个来,想想也挺麻烦的,
最后想想还有没有其它办法呢,出去走走后,就想,如果mysql帮我们排了的话,多方便呀。
那么mysql 排序在怎么用呢,ORDER BY + 字段 + 排序方式,这个不管怎么排也实现不了呀,
在一想能不能像where一样用呢,管他的,试试

 

说了这么多,怕有人看不懂,直接给需求来个图:
/

使用前:
SELECT `goods_id` FROM `sdb_goods` LIMIT 0 , 30

/

使用后
SELECT `goods_id` FROM `sdb_goods` ORDER BY `goods_id` = 4 DESC LIMIT 0 , 30
/

`goods_id` = 4 的商品真排上来了,这个样子就省事情了,哈哈

咱们再来完善下,

SELECT `goods_id` FROM `sdb_goods` ORDER BY `goods_id` = 4 DESC, `goods_id` ASC LIMIT 0 , 30

/

真没有想到,mysql还可以这样,威武!

来自: http://www.lpboke.com/mysql排序的妙用类似置顶.html

bitsCN.com
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 Article Tags

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)

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

Four recommended AI-assisted programming tools

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Learn how to develop mobile applications using Go language

Summary of the five most popular Go language libraries: essential tools for development Summary of the five most popular Go language libraries: essential tools for development Feb 22, 2024 pm 02:33 PM

Summary of the five most popular Go language libraries: essential tools for development

Understanding VSCode: What is this tool used for? Understanding VSCode: What is this tool used for? Mar 25, 2024 pm 03:06 PM

Understanding VSCode: What is this tool used for?

How to identify the authenticity of Dewu products? Methods to identify authenticity How to identify the authenticity of Dewu products? Methods to identify authenticity Mar 12, 2024 pm 12:16 PM

How to identify the authenticity of Dewu products? Methods to identify authenticity

Comprehensive Guide: Detailed Java Virtual Machine Installation Process Comprehensive Guide: Detailed Java Virtual Machine Installation Process Jan 24, 2024 am 09:02 AM

Comprehensive Guide: Detailed Java Virtual Machine Installation Process

Is PHP front-end or back-end in web development? Is PHP front-end or back-end in web development? Mar 24, 2024 pm 02:18 PM

Is PHP front-end or back-end in web development?

See all articles