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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:16:16+00:00 2026-05-27T03:16:16+00:00

I am trying to query 2 tables using the following sql statement in an

  • 0

I am trying to query 2 tables using the following sql statement in an attempt to return all records from each table that contains a specific id.

SELECT Phone.Phone, Email.Email FROM Contacts.Phone, Contacts.Email 
WHERE Phone.ContactId = :contactId AND Email.ContactId = :contactId

Contacts.Phone table contains 2 phone numbers for the given id and Contacts.Email contains 1 email for the given id. Using the above sql query, I get the following rows returned. This is just an example, of course, of a situation in where my result set from each table does match in number of rows.

Row 1: 555-555-5555 - email@email.com
Row 2: 666-666-6666 - email@email.com

The email is repeated in order to fill in the second row when I am trying to get:

Row 1: 555-555-5555 - email@email.com
Row 2: 666-666-6666 - NULL

I think I need to use a UNION to somehow join the tables, but I can’t figure out exactly how to write the sql statement. Another option would be to perform 2 separate SQL queries, which would be easier but I figure performance wise it would be better to collect all data I need in one query.

I am using MySQL.

  • 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-27T03:16:16+00:00Added an answer on May 27, 2026 at 3:16 am

    First of all, I think this:

    Another option would be to perform 2 separate SQL queries, which would be easier but I figure performance wise it would be better to collect all data I need in one query.

    is a bit misguided. The performance difference between one complex query and two simple queries will be quite small. That said, you suggested using a UNION; if you do want to use a single query, you can do this:

    SELECT 'EMAIL', Email.Email
      FROM Contacts.Email
     WHERE Email.ContactId = :contactId
    UNION ALL
    SELECT 'PHONE', Phone.Phone
      FROM Contacts.Phone
     WHERE Phone.ContactId = :contactId
     ORDER BY 1 -- put e-mail addresses before phone-numbers
    ;
    

    The first column will return the “type” of result, and the second column will have the datum.

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

Sidebar

Related Questions

I am trying to write a query that connects 3 tables. The first table
I'm trying to query a table, fetch all records, and save the result as
I am trying to select the following query from my table: SELECT ARTICLE_NO, USERNAME,
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
I am trying to execute the following SQL query using MS SQL Server Management
I am trying to use a prepared statement that deletes from two (temporary) tables:
I have the following SQL query that I am trying to run inside Microsoft
I'm trying to create a simple database table using the PHP MySQL query Create
I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into
I'm trying to query a mysql table which places its date in the following

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.