Mysql specifies the sorting Implementation Method for a row or column, and mysql specifies the sorting

Source: Internet
Author: User

Mysql specifies the sorting Implementation Method for a row or column, and mysql specifies the sorting

Method:

Desc:

Cannot achieve:

Method 1:

Select sp. productid, sp. productname, ss. sku from sp_product sp inner join sku_skus ss on sp. productid = ss. productid where sp. productname = 'lux skin whitening soap 115g' or sp. productname = 'crest (Crest) 180 grams of Green Tea mint toothpaste 'or sp. productname = 'flavor orchard orchid bean 190g/bag 1 bag of roasted bean casual snacks 'order by (case when sp. productid = 11042723 then 1 ELSE 2 END)

Method 2:

Core SQL

Select sp. productid, sp. productname, ss. sku from sp_product sp inner join sku_skus ss on sp. productid = ss. productid where sp. productname = 'lux skin whitening soap 115g' or sp. productname = 'crest (Crest) 180 grams of Green Tea mint toothpaste 'or sp. productname = 'flavor orchard orchid bean 190g/bag 1 bag of fried bean casual snacks 'order by sp. productid not in (11042723)

Mysql: puts the specified data row at the beginning
 
If it is just a data row, you can do this:

SELECT * FROM user ORDER BY user_id<>7,score DESC;  

This is mainly "user_id <> 7", and the user ID 7 is placed in the front.
 
For multiple data rows:

SELECT * FROM user ORDER BY user_id NOT IN(7,8,9),score DESC; 

The specified values in a mysql column are sorted in front of each other.
 
Top sorting of a single column:
Order by case

SELECT * FROM `jcxsw`.`t_company_product` order by (case when id=263 then 1 ELSE 4 END),category_id desc;

The SQL code first sorts the amount of data with id = 263 and then returns the result based on the category_id.
 
Sort multiple columns in front:

SELECT * FROM `web_membersfastsort_women` m order by m.province<>'10106000' , m.city<>'10106001' ,m.city desc,m.province desc,m.s_cid asc, m.images_ischeck desc,m.pic_num desc limit 2000,30

The front row of province = 10106000, and the front row of city = 10106000 in province = 10106001

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.