I’m having a bit of trouble figuring out how to make a query to achieve the following situation. I want a user to be able to view their events and activities (two different tables), along with any events/activities of organizations they belong to sorted by date
Events Table
eventID | eventTitle | eventBy | eventDate
1 Event 1 1 2012/10/11
2 Event 2 2 2012/10/08
3 Event 3 3 2012/10/05
Activities table
postID | postTitle | postBy | postDate
1 activity 1 1 2012/10/07
2 activity 2 2 2012/10/09
orgMembers table
orgID | userID
2 1 //userID 1 is a member of groupID 2
Anyway, I’m trying to get the results to look be the following if userid 1 is viewing the page
Title By Date
Activity 1 1 2012/10/07
Event 2 2 2012/10/08
Activity 2 2 2012/10/09
Event 1 1 2012/10/11
This query results in ONLY my posts only showing, not the organization that I’m a member of too.
select eventTitle as title, eventID as ids, eventBy as byUser, users.username, eventDate as date from events
inner join users on users.userid = events.eventBy
where eventBy in (select distinct g1.userID from orgMembers g1
inner join orgMembers g2 on g1.orgID = g2.orgID
where g2.userID = '$id')
or eventBy = '$id'
UNION
select postTitle as description, postID as ids, postBy as byUser, users.username, postDate as date from posts
inner join users on users.userid = posts.postBy
where postBy in (SELECT distinct g1.userID FROM orgMembers g1
inner join orgMembers g2 on g1.orgID = g2.orgID
where g2.userID = '$id')
or postBy = '$id'
Order BY date"
using
replace 1 with your user id
you might want to use
UNIONto select from 2 tables events and activities(select eventInfo as Ttile,eventBy as By, eventDate as Date from Events where Eventby in (SELECT userid FROM groups WHERE groupid = (SELECT groupid FROM groups WHERE userid = 1)))
UNION
(select activityInfo as Ttile,activityInfo as By, activityDate as Date from Activities where activityBy in (SELECT userid FROM groups WHERE groupid = (SELECT groupid FROM groups WHERE userid = 1))) Order BY Date
do the same with activities and the do union and sort