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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:35:45+00:00 2026-05-18T19:35:45+00:00

Or at least it’s complex for me, as I’m a total noob with SQL.

  • 0

Or at least it’s complex for me, as I’m a total noob with SQL.

I’d like to write a query for my MySQL-ODBC phonebook program, that returns certain details of a person and his primary phone number if there’s one.

Persons and numbers are stored in different tables (persons and numbers), while their relations are in the npr (number-person relations) table.
The primary key of ‘persons’ is ‘nick’, for ‘numbers’ it’s ‘number’, for ‘npr’ it’s ‘persons_nick’ and ‘numbers_number’ together.
If a number is primary, it’s signalled by the word “primary” being somewhere in the type attribute of the npr table. (I know it’s primitive, but I didn’t think I’d need this attribute and now I don’t have time to implement it properly)

Here’s the code as it is now:

SELECT persons.nick AS nick, persons.fullname AS fullname,
    persons.prefix AS prefix, persons.surname AS surname,
    persons.forename AS forename, persons.photo AS photo,
    numbers.prettynumber AS primarynumber
FROM persons
RIGHT JOIN npr ON npr.persons_nick=persons.nick
LEFT JOIN numbers ON npr.numbers_number=numbers.number
WHERE npr.type LIKE '%primary%'
ORDER BY nick;

This, of course doesn’t return anything if the person doesn’t have a primary phone number. How could I rewrite this query to return the person’s attributes and a void number in that case?

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-18T19:35:46+00:00Added an answer on May 18, 2026 at 7:35 pm

    The solution for your problem is to move the LIKE constraint into the predicate of the left join:

    SELECT persons.nick AS nick, persons.fullname AS fullname,
      persons.prefix AS prefix, persons.surname AS surname,
      persons.forename AS forename, persons.photo AS photo,
      numbers.prettynumber AS primarynumber
    FROM persons
    RIGHT JOIN npr ON npr.persons_nick=persons.nick
    LEFT JOIN numbers ON npr.numbers_number=numbers.number 
                         and
                         npr.type LIKE '%primary%'
    ORDER BY nick;
    

    The WHERE predicate filters all records but the join predicate belongs only to this left join. If the right side doesn’t match, then you still get the left side of the join.

    Update
    It seems to me that it would be better to only use left joins in this case. I don’t know the exact table definitions, but I assume that table npr is only for the tagged association between persons and numbers. So if you use only left joins then you can be sure, you get all persons whether or not they have a primary number.

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

Sidebar

Related Questions

I know that within a trigger - at least for SQL Server - one
I have seen at least one site in the past that had analytics on
What is the least amount of code you can write to create, sort (ascending),
After being told by at least 10 people on SO that version control was
At least in previous versions of SQL Server, you had to prefix Unicode string
There are at least 3 different keyboard layouts who are using the program i
I've configured (at least I've tried to configure) Lighty to enable the WebDAV plugin
Everyone (at least everyone who uses a compiled language) has faced compilation errors but
In .NET (at least in the 2008 version, and maybe in 2005 as well),
POSIX environments provide at least two ways of accessing files. There's the standard system

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.