mysql排序的妙用(类似置顶)_MySQL
Jun 01, 2016 pm 01:48 PMbitsCN.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
Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

Four recommended AI-assisted programming tools

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

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

Understanding VSCode: What is this tool used for?

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

Comprehensive Guide: Detailed Java Virtual Machine Installation Process

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