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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:44:24+00:00 2026-05-22T11:44:24+00:00

I have a table with 3 fields. principal , associate , status . How

  • 0

I have a table with 3 fields. principal, associate, status.

How can I determine whether the logged-in user is either a principal or an associate, and with how many people he is associated? I would also like to determine the status of the relationship.

query = "SELECT M.id, M.surname, M.firstname, R.principal_id
         , R.associate_id, R.status 
         FROM tbl_members M, tbl_relationship R WHERE
         -- ---------------------------------------------
         -- to make sure user exists in the members table
         -- ---------------------------------------------
         (R.principal_id = M.id OR R.associate_id = M.id) 
         AND (logged-in-user = R.associate_id OR logged-in-user = R.principal_id)
         AND R.status =1"

ERROR:- THIS LISTS times 2 of everybody in the relationship table.

Tables invloved:-
tbl_members (id, surname, firstname)
tbl_relationship (id, associate_id[FK tbl_member id], principal_id[FK tbl_member id])

associate_id | principal_id | status

1 3 1
1 4 1
2 1 0
2 3 0
5 1 1
6 1 1

From the above how many people are associated with logged-in-user(1)?

  • 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-22T11:44:25+00:00Added an answer on May 22, 2026 at 11:44 am

    Try:

    SELECT
          m.id, m.surname, m.firstname,
          COUNT(assoc.id) AS relationships_as_associate, 
          COUNT(princ.id) AS relationships_as_principal
    FROM tbl_members m
         LEFT JOIN tbl_relationship assoc 
              ON assoc.associate_id = m.id AND assoc.status = 1
         LEFT JOIN tbl_relationship princ 
              ON princ.principal_id = m.id AND princ.status = 1
    WHERE m.id = logged-in-user
    GROUP BY m.id
    

    (guessing that relationships with status = 0 can be ignored)

    If you only want the number of relationships this user is in (no matter the user’s role):

    SELECT
          m.id, m.surname, m.firstname,
          COUNT(rel.id) AS active_relationships
    FROM tbl_members m
         LEFT JOIN tbl_relationship rel
              ON (rel.associate_id = m.id OR rel.principal_id = m.id)
              AND rel.status = 1
    WHERE m.id = logged-in-user
    GROUP BY m.id
    

    EDIT:

    According to your comment, this is a query that will get all the users who are in relationship with the logged in user, with their details, and the role they are playing in the relationship (some of them are principals, others are associates):

    SELECT m.id, m.firstname, m.surname, temp.role
    FROM tbl_members m
    JOIN ((SELECT rel.principal_id as id, 'Principal' as role
           FROM tbl_relationship rel
           WHERE rel.associate_id = logged-in-user
           AND rel.status = 1)
         UNION
          (SELECT rel.associate_id as id, 'Associate' as role
           FROM tbl_relationship rel
           WHERE rel.principal_id = logged-in-user
           AND rel.status = 1)
         ) as temp
    ON temp.id = m.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.