简体   繁体   中英

SQL query to get merged result

I am trying to get the result from a table using MySQL query. What I want to do is I am trying to fetch 20 records from the table. In these 20 records, first 15 records must be in ascending order and the last 5 records must be in descending order. I am trying but I am not getting a way to get this done.

I don't know how to write sub queries.

Thanks.

You can use UNION ALL

(SELECT *
  FROM table1
 ORDER BY column_name 
 LIMIT 15)
 UNION ALL
(SELECT *
  FROM table1
 ORDER BY column_name DESC
 LIMIT 5)

Here is SQLFiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM