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)?
Try:
(guessing that relationships with
status = 0can be ignored)If you only want the number of relationships this user is in (no matter the user’s role):
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):