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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:57:55+00:00 2026-05-28T13:57:55+00:00

I have been recently going through my database code trying to improve on my

  • 0

I have been recently going through my database code trying to improve on my old code with new techniques to make it more efficient. Recently I have been looking to JOINs and I spotted what I thought was a perfect opportunity to test it out.

I am currently using the following two queries to form a list of groups for a user:

SELECT g.group_id, g. name 
FROM assigned_groups ag, user_groups g 
WHERE ag.user_id=:user_id AND ag.group_id=g.group_id

SELECT g.group_id, g. name 
FROM users u, user_groups g 
WHERE u.user_id=:user_id AND u.base_group=g.group_id

Those two select statements give me the list but I would like to join them into one. The only catch is a user may or may not have any groups listed in “assigned_groups”. Ie thats optional.

From what I understand that means I need a LEFT or RIGHT join. I have currently the following syntax:

SELECT g.group_id, g. name 
FROM 
  user_groups g, users u 
  LEFT JOIN assigned_groups ag ON ag.user_id=:user_id AND ag.group_id=g.group_id 
WHERE 
 u.base_group=g.group_id AND u.user_id=:user_id

However this is giving me the following error:
Unknown column ‘g.group_id’ in ‘on clause’

Here is an image of my table structure:
enter image description 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-28T13:57:56+00:00Added an answer on May 28, 2026 at 1:57 pm

    Change from implicit to explicit JOIN.

    This makes the g alias available later on in the FROM clause.

    SELECT g.group_id, g.name, 
        ag.name -- guessing here
    FROM 
      users u
      JOIN
      user_groups g ON u.base_group = g.group_id 
      LEFT JOIN 
      assigned_groups ag ON ag.user_id = :user_id AND ag.group_id = g.group_id 
    WHERE 
      u.user_id = :user_id
    

    Note: this also removes ambiguity in “which join to do first”

    However, I think you really want this. It depends on the relationship between u and ag

    Edit 2, after more comments

    SELECT g.group_id, g.name , 1 as IsBaseGroup
    FROM 
      users u
      JOIN
      user_groups g ON u.base_group = g.group_id 
    WHERE 
      u.user_id = :user_id
    UNION
    SELECT ag.group_id, ag.name, 0 as IsBaseGroup
    FROM 
      users u
      JOIN 
      assigned_groups ag ON u.user_id = ag.user_id
      JOIN
      user_groups g ON ag.group_id  = g.group_id 
    WHERE 
      u.user_id = :user_id;
    

    Edit, after comment.

    SELECT 
       g.group_id, g.name, 
       CASE WHEN u.base_group = g.group_id THEN 1 ELSE 0 END AS IsBaseGroup
    FROM 
      users u
      JOIN 
      assigned_groups ag ON u.user_id = ag.user_id
      JOIN
      user_groups g ON ag.group_id  = g.group_id 
    WHERE 
      u.user_id = :user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been going through some of our windows python 2.4 code and
I'm relatively new to cross-platform mobile development, and recently have been going through some
Recently, I have been going through search trees and I encountered red-black trees, the
Recently, I was going through an open-source project and although I have been developing
Recently I have been going through a project in an attempt to sanitize several
I've been learning more about Python recently, and as I was going through the
I've switched over to a Mac recently and, although things have been going quite
So recently I have been trying to set up a Vim-based iOS workflow. I
I have been tasked with going through a number of ColdFusion sites that have
I've been going through Head First Design Patterns (just came in recently) and I

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.