PHP Propel ORM MySQL - 多对多的左连接

发布于 2024-11-19 18:34:24 字数 1988 浏览 3 评论 0原文

我有两张表,一张称为会议,一张称为出勤,出勤是一个多对多关系数据库,格式如下:

Attendance:
    user_id | meeting_id | invited
    --------+------------+--------
    1       | 5          | 1
    2       | 5          | 0
    3       | 4          | 0
    3       | 5          | 1
    3       | 6          | 0

会议采用以下格式:

Meetings:
    meeting_id | meeting_name | owner_id
    -----------+--------------+----------
    3          | Awesome      | 2
    4          | Boring       | 2
    5          | Cool         | 5
    9          | Sexy         | 3

每个会议只能有一个会议行,但每个会议的出席行数不受限制(有限)每次会议的每个可能的用户)。

如何在 SQL 和/或 Propel 中创建一些内容来列出所有会议,其中(提供的)用户 ID 是会议中的所有者 ID,或者是出席数据库中的用户 ID 和受邀者。

我在搜索 userid 3 时正在寻找结果(基于上述数据):

Result for userid3:
    meeting_id | meeting_name | owner_id
    -----------+--------------+----------
    5          | Cool         | 5     - Because userid 3 is attending meeting 5
    9          | Sexy         | 3     - Because userid 3 owns meeting 9

我目前有以下内容,但实际上并不起作用,并且每个会议产生多行(因为该会议在出席数据库中存在不止一次) )。

$criteria->addJoin(MeetingMeetingsPeer::ID, MeetingAttendancePeer::MEETING_ID, Criteria::LEFT_JOIN);

$criterion = $criteria->getNewCriterion(MeetingMeetingsPeer::OWNER_ID, Meeting::getUserId());
$criterion->addOr($criteria->getNewCriterion(MeetingAttendancePeer::USER_ID, Meeting::getUserId()));

$criteria->add($criterion); 
return $criteria;

SQL 如下所示:

SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT FROM `meeting_meetings` LEFT JOIN meeting_attendance ON (meeting_meetings.ID=meeting_attendance.MEETING_ID) WHERE (meeting_meetings.OWNER_ID=1 OR meeting_attendance.USER_ID=1) 

感谢您的宝贵时间,

I have two tables one called meeting and one called attendance, attendance is a many to many relational database in the following format:

Attendance:
    user_id | meeting_id | invited
    --------+------------+--------
    1       | 5          | 1
    2       | 5          | 0
    3       | 4          | 0
    3       | 5          | 1
    3       | 6          | 0

Meetings are in the following format:

Meetings:
    meeting_id | meeting_name | owner_id
    -----------+--------------+----------
    3          | Awesome      | 2
    4          | Boring       | 2
    5          | Cool         | 5
    9          | Sexy         | 3

There can only be one meeting row per meeting, but unlimited attendance rows per meeting (limited to for every possible user for every meeting).

How in SQL and/or Propel do I create something that would list all meetings where the (provided) user_id is either the owner_id in meetings OR were the user_id and invited in the attendance database.

I am looking for a result (based on the above data) when searching for userid 3 of:

Result for userid3:
    meeting_id | meeting_name | owner_id
    -----------+--------------+----------
    5          | Cool         | 5     - Because userid 3 is attending meeting 5
    9          | Sexy         | 3     - Because userid 3 owns meeting 9

I currently have the following which doesn't work really, and produces multiple rows per meeting (because the meeting exists more than once in the attendance DB).

$criteria->addJoin(MeetingMeetingsPeer::ID, MeetingAttendancePeer::MEETING_ID, Criteria::LEFT_JOIN);

$criterion = $criteria->getNewCriterion(MeetingMeetingsPeer::OWNER_ID, Meeting::getUserId());
$criterion->addOr($criteria->getNewCriterion(MeetingAttendancePeer::USER_ID, Meeting::getUserId()));

$criteria->add($criterion); 
return $criteria;

Which is something like the below in SQL:

SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT FROM `meeting_meetings` LEFT JOIN meeting_attendance ON (meeting_meetings.ID=meeting_attendance.MEETING_ID) WHERE (meeting_meetings.OWNER_ID=1 OR meeting_attendance.USER_ID=1) 

Thanks for your time,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-11-26 18:34:24

这应该会获得 user_id 1 拥有的所有会议,以及 user_id 1 参加的所有会议。

SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, 
meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, 
meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT 
FROM `meeting_meetings`
WHERE `meeting_meetings`.`owner_id` = 1
UNION DISTINCT
SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, 
meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, 
meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT 
FROM `meeting_meetings`
JOIN `meeting_attendance` ON `meeting_meetings`.`meeting_id` = `meeting_attendance`.`meeting_id` AND `meeting_attendance`.`invited`
WHERE `meeting_attendance`.`user_id` = 1

这有点笨拙。就我个人而言,我会考虑向 meetings_attendance 表添加一个 owner 标志。

This should get you all meetings that are owned by user_id 1, as well as all meetings that are attended by user_id 1.

SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, 
meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, 
meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT 
FROM `meeting_meetings`
WHERE `meeting_meetings`.`owner_id` = 1
UNION DISTINCT
SELECT meeting_meetings.ID, meeting_meetings.OWNER_ID, meeting_meetings.GROUP_ID, meeting_meetings.NAME, 
meeting_meetings.COMPLETED, meeting_meetings.LOCATION, meeting_meetings.START, meeting_meetings.LENGTH, 
meeting_meetings.CREATED_AT, meeting_meetings.UPDATED_AT 
FROM `meeting_meetings`
JOIN `meeting_attendance` ON `meeting_meetings`.`meeting_id` = `meeting_attendance`.`meeting_id` AND `meeting_attendance`.`invited`
WHERE `meeting_attendance`.`user_id` = 1

It's a bit clunky. Personally, I would consider adding an owner flag to the meetings_attendance table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文