I have 3 tables something like this..
Table1.
group_id admin
................
1 x
2 y
3 z
Table2
group_id user
....................
1 a
1 b
2 d
Table3
user status
......................
x hi
y hello
z oh
a oho
b sss
d oops
now you can see that in the above tables we have group id
but in the first table i have group admin and second one contains group members.. and following is the result which we want something like this in which both group admin
as well as group members
we want from a signle group.
group_id user status
....................................
1 x hi
1 a oho
1 b sss
So,,,Help us to get result something like above.. in mysql
If you think of data in terms of data sets...
you have data in tables 1 and 2 that match in columns but you need them together. So using a UNION statement you can combine the two tables, alias the result set and join it to your third table to get the desired results.
This generates a derived table aka inline view (aliased t2)
SELECT T2.Group_ID, T2.user, T3.Status
FROM (SELECT group_ID, Admin as user
FROM table1
UNION ALL
SELECT group_ID, user
FROM table2
) t2
INNER JOIN table3 t3
on t3.group_Id = T2.GroupID
Try this?
SELECT table1.group_id, table3.user, table3.status
FROM Table1, table2
LEFT JOIN table3 ON (table1.admin = table3.user OR table2.user = table3.user)
WHERE table1.group_id = table2.group_id AND table1.group_id = 1
GROUP BY table3.user
I am assuming every group has an admin and every user has an entry in table 3.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.