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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:12:27+00:00 2026-05-30T05:12:27+00:00

I have a database table called ‘ members ‘. Assigned to members is a

  • 0

I have a database table called ‘members‘. Assigned to members is a position.

Positions come from Departments. I have Departments, then Sub-Departments within those and Positions within the Sub-Departments.

Hopefully I haven’t lost you.

What I want to do is is select all the members within a department, excluding any of those members from one position within that department.

“departments” Table: (includes departments and sub-departments)

ID     NAME                  PARENT_ID
1      Department #1         NULL
2      Department #2         NULL
3      Sub Department #1     1       
4      Sub Department #2     1       
5      Sub Department #3     2   

“departments_positions” Table:

ID     NAME             SUB_DEPARTMENT_ID
1      Position #1      3
2      Position #2      3
3      Position #3      4    

“members_positions” Table:

ID     MEMBER_ID     POSITION_ID
1      1             1
2      1             2
3      1             3         

Here’s my MySQL code:

SELECT m.* FROM members AS m 
    LEFT JOIN members_positions AS mp ON mp.member_id = m.id
    LEFT JOIN departments_positions AS dp ON mp.position_id = dp.id
    LEFT JOIN departments AS sd ON dp.sub_department_id = sd.id
    LEFT JOIN departments AS d ON sd.parent_id = d.id
    WHERE mp.position_id != '2' AND d.id = '1'
    GROUP BY m.id

So as you can see I want to extract the members that are in “Department #1” (ID: 1 from departments_table) but not members who are in Position #2 (ID: 2 from departments_positions table) of that department.

Hopefully I am making sense. My code above doesn’t seem to work. It just exports everyone from the department.

  • 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-30T05:12:29+00:00Added an answer on May 30, 2026 at 5:12 am

    It’s because you are using LEFT JOIN, which will not filter data.

    Change your query to use INNER JOIN.

    You also cannot use a select * if you’re grouping by member id.

    Edit: I inputed your tables into a local mysql db and confirmed this query working.

    SELECT DISTINCT mp.member_id
    FROM        members_positions mp
    INNER JOIN  departments_positions dp ON dp.id = mp.position_id
        AND     mp.position_id != 2
    INNER JOIN  departments sd on sd.id = dp.sub_department_id
        AND     sd.parent_id = 1
    GROUP BY    mp.member_id
    

    Also, any reason why you’re quoting your int values 2 & 1 in the where statement?

    If you only want the unique member id’s, you don’t need to do a GROUP BY. You should switch your select to this and then use an outer select to get all the member info:

    SELECT m.* 
    FROM members m
    INNER JOIN (
        SELECT DISTINCT mp.member_id
        ... the rest...
    ) innerQ ON innerQ.member_id = m.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm binding a gridview dynamically from a table in my database I have called
I have a database table called Posts which stores all the information regarding an
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
Say I have a database called Poll and have these two table structures. poll
Lets say I have a table in a sql server 2000 database called TransactionType:
I have a database table called Genre with the following fields: Id (Seeded Primary
I have a MySQL database table called submission with a field called points with
I have database called wisherDB, and I have a table called tbltasks inside the
I have an database table called MenuItem (MenuItemID, ParentMenuItemID, MenuItemName). I want to use
Lets say I have a database table called Scrape possibly setup like: UserID (int)

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.