I’m trying to JOIN/UNION/whatever data from two tables based on a third which maps them together. Here is the idea:
Table USERS contains user ID, username, some other data.
Table GROUPS contains group ID, group name, some other data.
Table USERGROUPMAP contains a bunch of user IDs and group IDs mapped together which correspond to IDs in USERS and GROUPS. Each user can be in multiple groups, each group can have multiple users.
I have always done this by storing objects or CSV data in a single field, e.g. making a column in groups called MEMBERS and storing id1,id2,id3…but this is what I was presented with.
I can’t change the schema…I know this has something to do with JOINs or UNIONs or something but my SQL is very rusty, so can anyone help out here?
My maybe semi-correct idea –
SELECT USERS.uid,
GROUPS.gid,
USERGROUPMAP.uid,
USERGROUPMAP.gid
FROM USERS,
GROUPS,
USERGROUPMAP
JOIN ON ((USERS.uid = USERGROUPMAP.uid) AND (GROUPS.gid = USERGROUPMAP.gid))
Any tips? 🙁 .. Thanks guys.
Edit, expected IO
USERS table contains a bunch of UIDs … 1,2,3,4,5, so on
GROUPS table contains a bunch of GIDs, 1000,1001,1002 and so on
USERGROUPMAP contains tuples corresponding to which users are in which groups, eg (1,1002) (2,1000) (5,1003) (5,1000)
I am trying to get a list of groups a user is in, so for this example I want to get two rows of 5,1003 and 5,1000 … make sense?
The userID you want to search is the searchID in the query. You can replace userID with groupID if you are listing users for a group.