How to use union all in MySQL to get the union sort

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible reasons, the data stored in the table is difficult to meet the display requirements on the page. The previous project had a function to display article content. Articles were divided into three states: pending, published, and offline.

The values ​​of the field (PROMOTE_STATUS) used to determine the status in the data table are 0, 1, and 2 respectively. The initial requirement was that articles only be displayed as pending and published, with published being listed before pending, and the two states being sorted according to their own circumstances. This implementation is relatively simple and can be achieved with the following order by statement.

order by PROMOTE_STATUS desc, SEQUENCE_ID desc......

After the test, the product felt that this could be optimized, and the display of articles should be changed to published, to be published, and offline (yes, offline was suddenly required, and it was placed at the end very proudly). What should I do then? It is definitely not feasible to change the corresponding values ​​of PROMOTE_STATUS of released, pending release, and offline to 2, 1, and 0, because other colleagues also use this table. If the correspondence here is changed. The judgment logic of other colleagues' codes must be changed.

So I thought of union all, and then I also needed to implement the display order of the articles in the three states. Therefore, the final idea is to find out the data when PROMOTE_STATUS is 1, 0, and 2 respectively, and then sort by order according to the situation in each state, and finally return each subset to the page for display after union all.

The final SQL statement is as follows:

select
    PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
    PROMOTE_TITLE,
    RELEASE_DATE
    FROM (
      (SELECT
        PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
        PROMOTE_TITLE,
        RELEASE_DATE 
       FROM SYS_TEXT_PROMOTE
       WHERE
         ENABLED_FLAG = '1'
         AND PROMOTE_STATUS=1
         AND SORT_ID = #{params.sortId}
         order by SEQUENCE_ID DESC,LAST_UPDATE_DATE DESC) a)
union all
select
    PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
    PROMOTE_TITLE,
    RELEASE_DATE
    FROM (
      (SELECT
        PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
        PROMOTE_TITLE,
        RELEASE_DATE 
       FROM SYS_TEXT_PROMOTE
       WHERE
        ENABLED_FLAG = '1'
        AND PROMOTE_STATUS=2
        AND SORT_ID = #{params.sortId}
        order by RELEASE_DATE DESC,LAST_UPDATE_DATE DESC) b)
union all
select
    PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
    PROMOTE_TITLE,
    RELEASE_DATE
    FROM (
      (SELECT
        PROMOTE_ID,
    SEQUENCE_ID,
    PROMOTE_STATUS,
        PROMOTE_TITLE,
        RELEASE_DATE 
       FROM SYS_TEXT_PROMOTE
        WHERE
        ENABLED_FLAG = '1'
        AND PROMOTE_STATUS=0
        AND SORT_ID = #{params.sortId}
        order by RELEASE_DATE DESC,LAST_UPDATE_DATE DESC) c)

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL union syntax code example analysis
  • Study on the default rules of mySQL UNION operator
  • Basic usage of UNION and UNION ALL in MySQL
  • Brief analysis of MySQL union and union all
  • How to merge multiple rows of data into one row in mysql
  • MYSQL uses Union to merge the data of two tables and display them

<<:  How to monitor Linux server status

>>:  Detailed explanation of VueRouter routing

Recommend

A brief discussion on the perfect adaptation solution for Vue mobile terminal

Preface: Based on a recent medical mobile project...

JavaScript modularity explained

Table of contents Preface: 1. Concept 2. The bene...

Mybatis fuzzy query implementation method

Mybatis fuzzy query implementation method The rev...

Mysql 5.6 "implicit conversion" causes index failure and inaccurate data

background When performing a SQL query, I tried t...

Summary of SQL deduplication methods

When using SQL to extract data, we often encounte...

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

Zabbix monitors the process of Linux system services

Zabbix automatically discovers rules to monitor s...

The difference between the four file extensions .html, .htm, .shtml and .shtm

Many friends who have just started to make web pag...

Encoding problems and solutions when mysql associates two tables

When Mysql associates two tables, an error messag...

Solution for importing more data from MySQL into Hive

Original derivative command: bin/sqoop import -co...

Detailed explanation of Angular component life cycle (I)

Table of contents Overview 1. Hook calling order ...

The latest virtual machine VMware 14 installation tutorial

First, I will give you the VMware 14 activation c...

Solution to css3 transform transition jitter problem

transform: scale(); Scaling will cause jitter in ...

Html Select option How to make the default selection

Adding the attribute selected = "selected&quo...