Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:25:04+00:00 2026-05-26T17:25:04+00:00

I have two queries I would like to append in MySQL. I have found

  • 0

I have two queries I would like to append in MySQL. I have found that UNION can help me do this, but it is not perfect as it does not retain the order of each appended query. In clear, I would like to do this:

(SELECT name,city FROM people WHERE xxx ORDER BY yyy) 
UNION 
(SELECT name,city FROM people WHERE zzz ORDER BY www)

but SQL won’t execute on the ORDER statements when there is a UNION

One solution would be to add a bogus field to each subquery and order by that field first :

(SELECT name,city,'0' as bogus FROM people WHERE xxx) 
UNION 
(SELECT name,city,'1' as bogus FROM people WHERE zzz) 
ORDER by bogus ASC, wwwzzz

but this is deeply inefficient as the query has to go through all fields to sort on the bogus field.
Do you know of any workaround?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:25:05+00:00Added an answer on May 26, 2026 at 5:25 pm

    Have you tried to use a UNION ALL rather than UNION ?

    see: http://dev.mysql.com/doc/refman/5.0/en/union.html

    e.g:

    (SELECT name,city,'0' as bogus FROM people WHERE xxx) 
    UNION ALL
    (SELECT name,city,'1' as bogus FROM people WHERE zzz) 
    ORDER by bogus ASC, wwwzzz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two queries that I would like to have merged into one. The
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
I have two queries which i would like to combine There are 4 tables
The first thing i would like to say is that this is not exactly
I have two queries. The first query looks like this : SELECT subject_id, period,
I'm seeing OraclePreparedStatement executeQuery() exhibit serialization. That is, I have two queries that I
I have two complex rails (AR) queries coming from two different methods that I
I have multiple datasets that I would like to combine into one. There is
I need some help with a MySQL query. I have two tables, one with
I have two tables on different servers, and I'd like some help finding an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.