You can do this by adding a pseudo-column named rank to each select, that you can sort by first, before sorting by your other criteria, e.g.:
select * from ( select 1 as Rank, id, add_date from Table union all select 2 as Rank, id, add_date from Table where distance < 5 union all select 3 as Rank, id, add_date from Table where distance between 5 and 15 ) a order by rank, id, add_date desc
Related Posts:
- How to order by with union in SQL?
- The used SELECT statements have a different number of columns
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- What is the difference between UNION and UNION ALL?
- Unknown column in ‘field list’ error on MySQL Update query
- Unknown column in ‘field list’ error on MySQL Update query
- MySQL – Operand should contain 1 column(s)
- MySQL “CREATE TABLE IF NOT EXISTS” -> Error 1050
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL create table if not exists and insert record only if table was created
- How do I use properly CASE..WHEN in MySQL
- How can I do a FULL OUTER JOIN in MySQL?
- MySQL query String contains
- What’s the difference between VARCHAR and CHAR?
- Case statement in MySQL
- How to declare a variable in MySQL?
- MySQL Cannot Add Foreign Key Constraint
- How to split the name string in mysql?
- How do I import an SQL file using the command line in MySQL?
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- mysql Foreign key constraint is incorrectly formed error
- MySQL: Invalid use of group function
- How can I return pivot table output in MySQL?
- Compare dates in MySQL
- MySQL “WITH” clause
- What is the difference between JOIN and UNION?
- MySQL Multiple Joins in one query?
- Error Code: 2013. Lost connection to MySQL server during query
- MySQL syntax for Join Update
- Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
- how to drop partition without dropping data in MySQL?
- MySQL combine two columns into one column
- Simple way to calculate median with MySQL
- Error 1046 No database Selected, how to resolve?
- Algebra Relational sql GROUP BY SORT BY ORDER BY
- MySQL Error 1264: out of range value for column
- Remove duplicate rows in MySQL
- How to SUM and SUBTRACT using SQL?
- Cast from VARCHAR to INT – MySQL
- SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO [duplicate]
- How do you force mysql LIKE to be case sensitive?
- MySQL – UPDATE multiple rows with different values in one query
- How to create a MySQL hierarchical recursive query?
- ERROR 1148: The used command is not allowed with this MySQL version
- Insert into a MySQL table or update if exists
- How to round down to nearest integer in MySQL?
- MySQL starts with searching issue
- Query to convert from datetime to date mysql
- Is it possible to GROUP BY multiple columns using MySQL?
- MySQL error: key specification without a key length
- Best way to test if a row exists in a MySQL table
- Check if MySQL table exists without using “select from” syntax?
- MySQL equivalent of DECODE function in Oracle
- mysql update column with value from another table
- How to delete from multiple tables in MySQL?
- MySQL: Can’t create table (errno: 150)
- How to copy a row and insert in same table with a autoincrement field in MySQL?
- Join vs. sub-query
- Alternative to except in MySQL
- Equivalent of explode() to work with strings in MySQL
- ROW_NUMBER() in MySQ
- MySQL OPTIMIZE all tables?
- MySQL INSERT INTO table VALUES.. vs INSERT INTO table SET
- MySQL – Get row number on select
- MySQL error: Unknown column in ‘where clause’
- How to truncate the text returned for a column in a MySQL query
- How to order by column A and then by column B?
- MySQL LIKE IN()?
- What is SELF JOIN and when would you use it? [duplicate]
- How do I UPDATE from a SELECT in SQL Server?
- Why does NULL = NULL evaluate to false in SQL server
- ORA-00918: column ambiguously defined in SELECT *
- How do I format date and time on ssrs report?
- ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
- SQL selecting rows by most recent date with two unique columns
- Postgres Error: More than one row returned by a subquery used as an expression
- SQL statement not working – “Operand type clash: date is incompatible with int’
- using sql count in a case statement
- How to create id with AUTO_INCREMENT on Oracle?
- Text was truncated or one or more characters had no match in the target code page including the primary key in an unpivot
- How can I group by date time column without taking time into consideration
- select * from table where column = something or, when unavailable, column = something else
- Use wpdb->prepare for `order by` column name
- Cron While Editing Post
- wpdb_prepare with multiple or condition
- WP_Query adds “(wp_posts.ID = ‘0’)” so no results are returned
- Convert a category of posts to a custom post type using (MySQL) queries
- Front end access to a SQL data repository – recommended way?
- Use $wpdb or other PHP script method to find/replace in WP database
- Connect to Ms SQL Server
- MySQL query for taxonomy-meta
- REGEXP_REPLACE in post_contet
- Menu Order updating automatically
- How delete post_content records of specific category from phpmyadmin
- Fastest way to display 5000 post titles?
- Is it possible to utilize custom SQL for a single-post.php?
- How to handle my row actions on a custom list table in the admin section
- MySQL swap one table for another?
- How can I avoid duplicate primary keys in SQL import?
- Migrated to Namecheap, now Trouble with Database & wp-config [closed]