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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:25:02+00:00 2026-05-19T23:25:02+00:00

Given a fake example where there are contacts and for each contact there can

  • 0

Given a fake example where there are contacts and for each contact there can be one or zero Sms records; I can do this

SELECT Contact.id, Contact.name, Sms.provider, Sms.number FROM Contact, Sms WHERE Sms.contactId = Contact.id.

But I it needs to show a row even if there is no SMS number for that contact.

I know of two ways to accomplish this. One is with a UNION of two selects

(SELECT Contact.id, Contact.name, Sms.provider, Sms.number FROM Contact, Sms WHERE Sms.contactId = Contact.id) UNION (SELECT id, name, null provider, null number FROM Contact WHERE (SELECT 1 FROM Sms WHERE contactId = Contact.id) IS NULL).

and another is two substatements

SELECT id, name, (SELECT provider FROM Sms WHERE contactId = Contact.id) provider, (SELECT number FROM Sms WHERE contactId = Contact.id) number FROM Contact

The UNION method requires going through the database twice and the substatements method requires two lookups on the same table.

I run into this often and I prefer to have all the code in MySQL and not to work with the software code to accomplish this. Working with MySQL usually turns out to be more efficient as well as simpler. Many of the real situations I work with involve large databases.

Is there a way to get two fields from a related table with one query?

  • 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-19T23:25:03+00:00Added an answer on May 19, 2026 at 11:25 pm

    But I it needs to show a row even if
    there is no SMS number for that
    contact.

    Then use a LEFT OUTER JOIN, which returns a row from the left table even if there is no corresponding row in the right table.

    It’s a good idea to learn to always use JOIN syntax instead of the pseudo-inner-join when you just list tables with commas.

    SELECT 
      Contact.id, 
      Contact.name, 
      Sms.provider, 
      Sms.number
    FROM 
      Contact
    LEFT OUTER JOIN 
      Sms 
    ON
      Sms.contactId = Contact.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given this example, how do we ensure that the thread instance has been freed?
Given a Python object of any kind, is there an easy way to get
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a (source) patch file, what's the easiest way to apply this patch on
Given this HTML: <ul id=topnav> <li id=topnav_galleries><a href=#>Galleries</a></li> <li id=topnav_information><a href=#>Information</a></li> </ul> And this
Given a latitude and longitude, what is the easiest way to find the name
This is more of an opinion seeking question, so there may not be a
I found this while searching for how to fake multiple inheritance in PHP (since
I have a website which stores customer details, name, address, contact etc. A customer

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.