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 6947535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:42:19+00:00 2026-05-27T13:42:19+00:00

I am trying to UNION two columns in a SELECT, and alias to a

  • 0

I am trying to UNION two columns in a SELECT, and alias to a third.

I also need to retrieve the data matching the WHERE clause, then sort by the aliased column (MLS_SORT).

This, and variations of it that I have tried, don’t work.

SELECT *
FROM
(SELECT MLS_AGENT_ID AS MLS_SORT FROM mlsdata)

UNION

(SELECT MLS_OFFICE_ID AS MLS_SORT FROM mlsdata)

WHERE (MLS_AGENT_ID = $agent_narid) OR (MLS_OFFICE_ID = $office_narid)      

ORDER BY MLS_SORT

This part does work and creates the MLS_SORT alias with the correct values, but I can’t figure out how to limit the results to the WHERE clause above:

(SELECT MLS_AGENT_ID AS MLS_SORT FROM mlsdata)

UNION

(SELECT MLS_OFFICE_ID AS MLS_SORT FROM mlsdata)

Am I at least going down the correct path or is this not the proper way to proceed?

Thanks for any assistance.

  • 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-27T13:42:20+00:00Added an answer on May 27, 2026 at 1:42 pm

    The trick is to understand the syntax of UNION: query UNION query

    I think you want:

    SELECT MLS_SORT
    FROM
    (
        SELECT MLS_AGENT_ID AS MLS_SORT 
          FROM mlsdata
         WHERE MLS_AGENT_ID = $agent_narid
    
        UNION
    
        SELECT MLS_OFFICE_ID AS MLS_SORT 
          FROM mlsdata
         WHERE MLS_OFFICE_ID = $office_narid
    )
    ORDER BY MLS_SORT
    

    To get the two ID subsets into a single result set then sort them.

    But, this whole query looks like it’s going to give a two-row result set — one row for an agent and another for an office. Is that what you want?

    Your logic effectively typecasts agent id and office id numbers into a single result set. Does that make sense in your application?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to UNION the results of two queries. But I'm getting the following
I am trying to create a view for a UNION of 2 select statements
I am trying to retrieve two counts from two separate tables into one SQL
I'm trying to SUM two rows obtained from differente SQL queries in UNION, is
I have a query that performs a UNION between two tables. I'm trying to
I am trying to build a select query that will essentially left join two
I'm having tremendous difficulty trying to merge two columns from two tables in PL/SQL.
I am trying to find the union of two sets with map. I have
I'm trying to SELECT from two tables and ORDER BY date (a column they
I'm trying to create a Union between two tables, using HQL (Hibernate Query Language).

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.