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

  • Home
  • SEARCH
  • 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 7829149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:38:25+00:00 2026-06-02T10:38:25+00:00

I have an unusual SQL table (not mine) which has the following fields (among

  • 0

I have an unusual SQL table (not mine) which has the following fields (among others): last_name, primary_name, secondary_name, denoting married couples. The last name is assumed to be shared (not very modern, I know), and if it’s not a couple, then either the primary_name or secondary_name may be NULL. (The table also has several duplicates.)

What I want to do is get a list of all names (“first last”) in the database, alphabetized in the usual manner. Right now I’m doing two passes through the database using PHP and PDO:

$qstr = "SELECT DISTINCT primary_name, last_name 
            FROM members 
            WHERE primary_name IS NOT null
            ORDER BY last_name, primary_name";
$sth = $dbh->prepare($qstr);
$sth->execute();
// output the results

$qstr = "SELECT DISTINCT secondary_name, last_name 
            FROM members 
            WHERE secondary_name IS NOT null
            ORDER BY last_name, secondary_name";
$sth = $dbh->prepare($qstr);
$sth->execute();
// output the new results

But the end result isn’t alphabetized because the second pass starts over again.

How can I get all the names at once, alphabetized completely? Is there a way to do this in SQL, or do I need to build two arrays and re-alphabetize them in PHP afterwards?

EDIT
The database looks something like this:

last_name  primary_name   secondary_name
----------------------------------------
Abrams     Joe            Susan
Miller     Sam            Abby

The desired output would be something like this:

["Joe Abrams","Susan Abrams","Abby Miller","Sam Miller"]

Instead, if the first pass gets all the husbands and the second pass all the wives, I’m getting something like this:

["Joe Abrams","Sam Miller","Susan Abrams","Abby Miller"]
  • 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-06-02T10:38:27+00:00Added an answer on June 2, 2026 at 10:38 am

    An alternative is to use UNION…

    SELECT
      *
    FROM
    (
      SELECT primary_name AS pri_sec_name, last_name 
        FROM members 
       WHERE primary_name IS NOT null
    
      UNION
    
      SELECT secondary_name AS pri_sec_name, last_name 
        FROM members 
       WHERE secondary_name IS NOT null
    )
      AS data
    ORDER BY
      last_name, pri_sec_name
    

    NOTE: UNION (as opposed to UNION ALL) will de-duplicate the results.

    Another is to do a join on a mapping table.

    SELECT
      members.last_name,
      CASE WHEN map.mode = 1 THEN members.primary_name ELSE members.secondary_name END AS pri_sec_name
    FROM
      members
    INNER JOIN
      (SELECT 1 as mode UNION ALL SELECT 2 as mode) AS map
        ON (map.mode = 1 AND members.primary_name   IS NOT NULL)
        OR (map.mode = 2 AND members.secondary_name IS NOT NULL)
    ORDER BY
      1,
      2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unusual situation: I have an assembler file, which has .c extension
I have an unusual situation to model in a MS SQL Server database: the
Salute.. I have an unusual problem. Here in this table in MSDN library we
I have a very unusual problem. I have an ASP.NET application that uses LINQ-to-SQL
I have this unusual requirement from a client, and I am not quite sure
Have a matrix report now that has Position, Hours and Wages for a location
have not tested on windows. but in ubuntu when u disconnect from the network,
I have an unusual situation - I have an embedded video streaming device with
i have an really unusual problem i've never had before. i've no .htaccess file
I have a rather unusual problem, and it is hurting my brain. Problem: Given

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.