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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:34:11+00:00 2026-06-13T04:34:11+00:00

I have a group_names table which stores user groups. Each group has a manager,

  • 0

I have a “group_names” table which stores user groups. Each group has a manager, which can be either an individual user, or be managed by another group. This is currently determined by group_names.mgr_type. An INT value of 1 means that it’s an individual user and a value of 2 means that it’s a group that manages it.

For example, the “General Users” group could be managed by the “IT Department” group or by “John Doe”. Unique ID fields on the users and group_names tables are “user_id” and “group_id”.

I want to take the logic out of PHP and just put it in the SQL query, but I’m running into problems here. I’m not sure if this is possible given the table layout or if I’m just incapable of following simple instructions provided in the documentation.

SELECT gn.group_id, gn.group_name, ... gn.mgr_id, CONCAT(usr.user_firstname,' ',usr.user_lastname) AS created_name, usr.user_id,
CASE gn.mgr_type
    WHEN '1' THEN CONCAT(usr.user_firstname,' ',usr.user_lastname) WHERE usr.user_id=gn.mgr_id LIMIT 1
    WHEN '2' THEN gn.group_name WHERE gn.group_id=gn.mgr_id LIMIT 1
END AS mgr_name
FROM group_names gn 
LEFT JOIN users usr 
ON gn.created_by=usr.user_id 
ORDER BY group_name ASC;

I’ve tried different variations of IF and CASE, with and without parentheses, but keep getting the “invalid syntax” error.

...for the right syntax to use near 'WHERE usr.user_id=gn.mgr_id LIMIT 1) WHEN '2' THEN (gn.group_name WHERE gn' at line 3

Can I even select a different row from the same table (select the group_name from a different row based on the mgr_id=group_id), or should I add another table?
Any suggestions on an easier/more efficient way to do this?

Thanks!

  • 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-06-13T04:34:12+00:00Added an answer on June 13, 2026 at 4:34 am

    Have you tried this since you already joined to those tables

    CASE gn.mgr_type
        WHEN '1' THEN CONCAT(mgr_usr .user_firstname,' ',mgr_usr .user_lastname)
        WHEN '2' THEN mgr_grp.group_name
    END AS mgr_name
    

    You actually need 2 more left joins for that to work

    LEFT JOIN users mgr_usr 
       ON gn.mgr_id = mgr_usr.user_id 
    LEFT JOIN group_names mgr_grp
       ON gn.mgr_id = mgr_grp.group_id
    

    Left join to the user table to get the manager name if it’s user or join to group_names table to retrieve the group name if it’s a group

    You can restrict the JOINs further

    LEFT JOIN users mgr_usr 
       ON gn.mgr_id = mgr_usr.user_id AND gn.mgr_type = 1
    LEFT JOIN group_names mgr_grp
       ON gn.mgr_id = mgr_grp.group_id AND gn.mgr_type = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an oracle table which has a column that stores XML in a
I have the following two table: -Groups- Id Name -Members- Id GroupId (Group.Id is
I have a table named 'data' which stores the email addresses and city of
I have a table like groups that stores a list of groups that my
I have a table that includes some student group name, lesson time, day names
I have a table like this: name code group john 12 smith 15 how
I have the following two tables in my mysql database. Table Name: groups id
I have an entity mapped as following: @Entity @Table(name = Groups) @IdClass(value = GroupId.class)
I have a group name which contains special characters. CN=IN&T DC Gebnn/Dohn,OU=ABGroups,OU=Hammers,DC=MyCompany,DC=int I am
I have two entity groups. Restaurants and Users. Restaurants can be rated (1-5) by

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.