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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:19:01+00:00 2026-05-31T01:19:01+00:00

I have the tables users , locations and a join table. The join table

  • 0

I have the tables users, locations and a join table. The join table also has a rank value for each individual at that location, because those individuals have higher ranks at certain locations than at other locations.

users
=========
id | name
=========
 1 | john
 2 | bob
 3 | alex

locations
===============
id | name
===============
 1 | san diego
 2 | dallas
 3 | new york
 4 | denver

join_users_locations
==============================
user_id | location_id | rank
==============================
      2 |           1 | 4
      2 |           2 | 3
      2 |           4 | 2
      3 |           1 | 2
      3 |           2 | 4

You can see that in the join table a user is only listed if they have a rank at that location. They are not even in the table if their rank is zero/null.

I’m wanting to get query results like this:

name |  location | rank 
=======================
 bob | san diego | 4
 bob |    dallas | 3
 bob |  new york | 0
 bob |    denver | 2

As you can see, I want all locations to be in the list, even ones that the user isn’t joined with (just give those fields a rank value of zero).

It’s easy to get the list of locations and ranks for locations he/she is joined with:

SELECT
    u.name,
FROM users u
LEFT JOIN join_users_locations jul ON jul.user_id = u.id
LEFT JOIN locations l ON l.id = jul.location_id
WHERE u.id = 2

But that only lists what locations the user is joined with. I want to see the non-joined locations as well.

I know this is possible, but I’m not sure what the solution is. I’ve tried some different types of JOINs with various results.

SQL FIDDLE HERE

  • 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-31T01:19:03+00:00Added an answer on May 31, 2026 at 1:19 am

    You can do this with a CROSS JOIN from users to locations.

    SELECT
        u.name,
        l.name AS location,
        IFNULL(jul.rank, 0) AS rank
    FROM
        users u CROSS JOIN
        locations l LEFT JOIN
        join_users_locations jul 
            ON jul.user_id = u.ID 
            AND jul.location_id = l.id
    WHERE u.id = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, locations and location groups CREATE TABLE locations ( location_id INT
I have table with 50 entries (users with such details like Name Surname Location
I have a table of users that I want other users to be able
I have a table Admins, which creates a relationship between Users and Locations: ID
I have these two database tables: locations id name users id location_id last_name first_name
I have two tables user and location user has id(primary) and location fields/columns location
I have two tables: users and user_friends with a 1:1 reflective join on the
Is it possible to do a join between 2 tables that don't have foreign
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many

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.