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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:03:21+00:00 2026-05-24T05:03:21+00:00

Been coding for 48 hours straight and am banging my head against the wall

  • 0

Been coding for 48 hours straight and am banging my head against the wall here. Please help me with this small issue.

My SQL query is this:

SELECT u.Firstname, u.Lastname, u.Rep, u.Email, u.Password, u.Gender, u.Level,
       u.Birthday, u.Achievements, u.Height, u.Unit, u.cityid, u.countryid,
       r.RegDate, ci.Name AS City, co.Name AS Country
FROM Users u, Registry r, Cities ci, Countries co
WHERE u.id = 1 AND r.uid = u.id AND u.cityid = ci.id AND u.countryid = co.id
LIMIT 1

My problem is that I just noticed that sometimes Users.cityid and Users.countryid are NULL (which is OK).

I want the query to give me all the other info (like, return NULL for City and Country) for this user even if one or both those fields are NULL. How to make the AND-parts conditional?

I hope I’m making myself clear in my fogginess.

  • 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-24T05:03:22+00:00Added an answer on May 24, 2026 at 5:03 am

    You need to use a LEFT JOIN on your tables instead of using a WHERE.

    So your FROM turns into:

    FROM Users u JOIN Registry r on u.id = r.uid 
    LEFT JOIN Cities ci ON u.cityid = ci.id
    LEFT JOIN Countries co ON u.countryid = co.id
    WHERE u.id = 1 LIMIT 1
    

    The LEFT JOIN is an OUTER join; it will join across the tables where the leftmost (hence the LEFT in the JOIN) term in the JOIN (i.e., the first one that appears) has an entry but the other table does not. OUTER JOINs are useful for these situations where you don’t necessarily have data entries in all the tables for what you want from a query; they can be confusing at first, but they become very important to using SQL well.

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

Sidebar

Related Questions

I've been beating my head against this absolutely infuriating bug for the last 48
It's 12:30am and I have been coding for 9 hours straight. I really need
I've been looking for a solution to this for hours, but I can't find
OK, so I've been working on this for hours. I've found a couple of
First, forgive me if this has been answered already. I spent a few hours
Been looking around for about 4 hours so if the answer is here somewhere
I've been coding alot of web-stuff all my life, rails lately. And i can
I've been coding in Ruby for sometime now, but I don't understand when to
I've been coding for some time, and I always thought the linking phase of
I've been coding a Windows app chess game in C# as an exercise in

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.