简体   繁体   English

SQL查询获取合并结果

[英]SQL query to get merged result

I am trying to get the result from a table using MySQL query. 我正在尝试使用MySQL查询从表中获取结果。 What I want to do is I am trying to fetch 20 records from the table. 我想做的是尝试从表中获取20条记录。 In these 20 records, first 15 records must be in ascending order and the last 5 records must be in descending order. 在这20条记录中,前15条记录必须按升序排列,后5条记录必须按降序排列。 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 您可以使用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 这是SQLFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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