I have 4 tables, USER(ID, USER_ID, NAME), USER_2(ID, USER_ID, EMAIL)
and USER_CLASS_MAP(ID, USER_ID, CLASS_ID)
and CLASS(ID, NAME)
. USER_CLASS_MAP
is basically to map MANY-MANY relation between USER
and CLASS
.
Need to send all the details of all the users belonging to a particular class. I have non-working code as below- any idea what I might be doing wrong, or is there any other efficient way to achieve the same?
I am referring (SQL Inner-join with 3 tables?) for INNER JOIN over multiple tables, but here the issue is that instead of a single user against the given CLASS_ID, I get a list of USER_ID
from USER_CLASS_MAP
.
SELECT USER_ID from USER_CLASS_MAP where CLASS_ID=:classID
This is what I currently have:
SELECT USER1.NAME, USER2.EMAIL
INNER JOIN CLASS_USER_MAP as cmap
ON cmap.USER_ID = USER1.ID
INNER JOIN CLASS_USER_MAP as cmap
ON cmap.USER_ID = USER2.ID
The problem here is that .ID is basically a list!
EXAMPLE:
USER:
(id1, user1, rob)
(id2, user2, bob)
USER_2:
(id1, user1, rob@something.something)
(id2, user2, bob@something.something)
USER_CLASS_MAP:
(id1, user1, class1)
(id2, user2, class1)
CLASS:
(class1, Biology)
(class2, Chemistry)
Given:
Get all User Details for class with classId = class1
Output:
[
{USER_ID=user1, NAME=rob, EMAIL=rob@something.something},
{USER_ID=user2, NAME=bob, EMAIL=bob@something.something}
]