How to sort a row or column in mysql

How to sort a row or column in mysql

method:

By desc:

Neither can be achieved:

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 Forever Rejuvenating Soap 115g'
or sp.productname='Crest Green Tea Mint Toothpaste 180g'
or sp.productname='Xinwei Orchard Orchid Beans 190g/bag 1 bag of roasted beans and 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 Forever Rejuvenating Soap 115g'
or sp.productname='Crest Green Tea Mint Toothpaste 180g'
or sp.productname='Xinwei Orchard Orchid Beans 190g/bag 1 bag roasted beans snacks' ORDER BY sp.productid not in(11042723)

Mysql: Put the specified data row in front

If it is just one row of data, you can do this:

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

Mainly "user_id<>7", the user with id 7 will be ranked first.

If there are multiple rows of data:

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

mysql sort a column by specifying the value first

Sort a single column first:
mysql sort by case

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

This SQL code will first sort the data with id = 263 and then sort in reverse order according to category_id

Sort multiple columns first:

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 first row of province = 10106000, the first row of city = 10106001 in province = 10106000

You may also be interested in:
  • How to convert a column of comma-separated values ​​into columns in MySQL
  • Detailed explanation of MySQL row locks when encountering composite primary keys and multi-column indexes
  • Mysql method to calculate the difference between two adjacent rows of a column
  • MySQL column to row conversion, method of merging fields (must read)
  • MySQL column to row conversion and year-month grouping example
  • MySQL column to row conversion tips (share)
  • Detailed examples of converting rows to columns and columns to rows in MySQL
  • Implementation of dynamic conversion of mysql rows and columns (contingency table, cross table)
  • Database implementation of row and column conversion (mysql example)
  • How to convert rows to columns in MySQL

<<:  Detailed explanation of the correct way to configure SSL (https certificate) in Apache on Ubuntu

>>:  TimePicker in element disables part of the time (disabled to minutes)

Recommend

Deploy Varnish cache proxy server based on Centos7

1. Varnish Overview 1. Introduction to Varnish Va...

Linux file system operation implementation

This reading note mainly records the operations r...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

Linux C log output code template sample code

Preface This article mainly introduces the releva...

Vue project realizes login and registration effect

This article example shares the specific code of ...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Linux file and user management practice

1. Display the files or directories in the /etc d...

How to query and update the same table in MySQL database at the same time

In ordinary projects, I often encounter this prob...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

How to invert the implementation of a Bezier curve in CSS

First, let’s take a look at a CSS carousel animat...

JavaScript to implement the web version of the snake game

This article shares the specific code for JavaScr...

Analyze the difference between computed and watch in Vue

Table of contents 1. Introduction to computed 1.1...

Html/Css (the first must-read guide for beginners)

1. Understanding the meaning of web standards-Why...

How to add vector icons to web font files in web page production

As we all know, there are two types of images in c...