简体   繁体   English

MySQL使用PHP查询多个用户和多个表

[英]MySQL query for multiple users and multiple tables with PHP

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 无论如何,如果用户ID 1正在查看页面,我正试图让结果看起来如下

 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 运用

WHERE eventby IN 

replace 1 with your user id 用您的用户ID替换1

you might want to use UNION to select from 2 tables events and activities 您可能希望使用UNION从2个表事件和活动中进行选择

(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 (选择eventInfo作为Ttile,eventBy作为By,eventDate作为事件中的日期,其中Eventby in(SELECT userid FROM groups WHERE groupid =(SELECT groupid FROM groups WHERE userid = 1)))UNION(选择activityInfo作为Ttile,activityInfo作为By,activityDate作为活动日期,其中activityBy in(SELECT userid FROM groups WHERE groupid =(SELECT groupid FROM groups WHERE userid = 1)))订单BY日期

do the same with activities and the do union and sort 对活动和做联合和排序做同样的事情

The OR eventBy = 1 and OR activityBy = 1 must be added if one user can be "without group". 如果一个用户可以“没有组”,则必须添加OR eventBy = 1OR activityBy = 1 If all users have at least one group, you can remove them 如果所有用户至少有一个组,则可以将其删除

SELECT eventInfo as title, eventBy as By, eventDate as Date
FROM event
WHERE eventBy IN (select distinct g1.userId from groups g1
                   inner join groups g2 on g1.groupId = g2.groupId
                   where g2.userId = 1)
OR eventBy = 1 

UNION
SELECT activityInfo as title, activityBy as By, activityDate as Date
FROM Activity
WHERE activityBy = (select distinct g1.userId from groups g1
                   inner join groups g2 on g1.groupId = g2.groupId
                   where g2.userId = 1)
OR activityBy = 1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM