A brief discussion on Mysql specified order sorting query

A brief discussion on Mysql specified order sorting query

Recently, I have been working on a large-screen display project, similar to the one that displays flight information at airports and train stations, but the content displayed is much more complicated. Some of the data are as follows:

The front-end mainly uses the Vue framework, requiring the back-end data to be displayed on the front-end.

First: The factory entry and exit indicators are not fixed (may be 6, or may be 7 or 8 in the future);

Second: Use slashes instead of "numeric" values;

Third: The names of the inbound and outbound indicators must conform to the names specified in the figure above (the names queried from the back-end database are different from the names specified in the front-end, and the units contain ">1", ">0.3", etc., so they cannot be queried directly from the back-end. The name + unit is placed on the front-end and needs to be processed by yourself); Fourth: The order in which the back-end data is transmitted must be the order specified by the front-end. Anyway, the requirement is that the front end only needs to loop out the data, and the back end takes care of all other needs.

There are two tables: table_a, table_b

Step 1: Create a table based on table_a.id=table_b.ids

SQL statement: SELECT * FROM (SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.ids)A, take the underlined part as a new table A

Step 2: It is found that Xiaobailong and Sha Wujing are not in the database, so they need to be added:

SQL statement:

SELECT * FROM (SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.ids
UNION ALL
SELECT 8 id,'白龙马' `name`,'小白' sname,'1000' age ,8 ids, 27 `value`
UNION ALL
SELECT 9 id,'Sha Wujing' `name`,'Juanlian General' sname,'2000' age ,9 ids, 289 `value`
)A

The underlined part is used to add some missing data, which is sometimes used for fixed display needs on the front end.

Step 3: Please arrange in the order of Tang Monk, Sun Wukong, Zhu Bajie, Sha Wujing, Xiao Bailong, Chunhua, Wang Wu, Zhao Liu, Tang Qi

SQL statement:

SELECT * FROM (SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.ids
UNION ALL
SELECT 8 id,'白龙马' `name`,'小白' sname,'1000' age ,8 ids, 27 `value`
UNION ALL
SELECT 9 id,'Sha Wujing' `name`,'Juanlian General' sname,'2000' age ,9 ids, 289 `value`)A
    INNER JOIN (SELECT 7 ids, 1 `order`
UNION ALL
SELECT 2 ids, 2 `order`
UNION ALL
SELECT 4 ids, 3 `order`
UNION ALL
SELECT 9 ids, 4 `order`
UNION ALL
SELECT 8 ids, 5 `order`
UNION ALL
SELECT 6 ids, 6 `order`
UNION ALL
SELECT 1 ids, 7 `order`
UNION ALL
SELECT 5 ids, 8 `order`
UNION ALL
SELECT 3 ids, 9`order` ) B ON B.ids=A.id
ORDER BY B.order

The underlined statements add two fields to the entire table A, thereby achieving a fixed sort in a specified manner. Fields can also be added to achieve other desired purposes.

The above is all the content of our introduction to MySQL specified order sorting query. If you still have any questions, you can discuss in the comment area below.

You may also be interested in:
  • Detailed explanation of MySQL single table query operation examples [syntax, constraints, grouping, aggregation, filtering, sorting, etc.]
  • Detailed explanation of the failure of MySQL to use UNION to connect two queries
  • Analysis of MySQL query sorting and query aggregation function usage
  • Yii2 implements cross-MySQL database association query sorting function code
  • Tutorial on how to sort query results and limit the number of results returned in MySQL
  • A brief tutorial on sorting MySQL query results by value
  • Database query sorting using random sorting results example (Oracle/MySQL/MS SQL Server)
  • MySQL query optimization: Introduction to join query sort limit (join, order by, limit statement)
  • MySQL query optimization: a brief discussion on join query sorting
  • MySQL query sorting and paging related

<<:  How to start and stop SpringBoot jar program deployment shell script in Linux

>>:  You Probably Don’t Need to Use Switch Statements in JavaScript

Recommend

Linux disk space release problem summary

The /partition utilization of a server in IDC is ...

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

Detailed explanation of the execution plan explain command example in MySQL

Preface The explain command is the primary way to...

Solve the problem of case sensitivity of Linux+Apache server URL

I encountered a problem today. When entering the ...

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

Ant designing vue table to achieve a complete example of scalable columns

Perfect solution to the scalable column problem o...

How to package the uniapp project as a desktop application

Installing Electron cnpm install electron -g Inst...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

Detailed explanation of MySQL's Seconds_Behind_Master

Table of contents Seconds_Behind_Master Original ...

The principle and implementation of two-way binding in Vue2.x

Table of contents 1. Implementation process 2. Di...

How to deeply understand React's ref attribute

Table of contents Overview 1. Creation of Refs ob...

Example of how to quickly build a LEMP environment with Docker

LEMP (Linux + Nginx + MySQL + PHP) is basically a...