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

How to insert a link in html

Each web page has an address, identified by a URL...

Specific use of MySQL global locks and table-level locks

Table of contents Preface Global Lock Table lock ...

Analyze the compilation and burning of Linux kernel and device tree

Table of contents 1. Prepare materials 2. Downloa...

How to use Vue+ElementUI Tree

The use of Vue+ElementUI Tree is for your referen...

Sample code for modifying the input prompt text style in html

On many websites, we have seen the input box disp...

How to use React slots

Table of contents need Core Idea Two ways to impl...

CentOS8 installation tutorial of jdk8 / java8 (recommended)

Preface At first, I wanted to use wget to downloa...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

How to create a view on multiple tables in MySQL

In MySQL, create a view on two or more base table...

How to configure Nginx's anti-hotlinking

Experimental environment • A minimally installed ...

Detailed explanation of Tomcat configuration and optimization solutions

Service.xml The Server.xml configuration file is ...

Detailed explanation of flex and position compatibility mining notes

Today I had some free time to write a website for...

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...