基于最大值的MySQL查询联接
精简了我的查询示例基于最大值的MySQL查询联接,mysql,sql,join,Mysql,Sql,Join,精简了我的查询示例 SELECT ce.*, cev.* FROM calendar_event ce INNER JOIN calendar_event_viewing cev on ce.calendar_event_id = cev.calendar_event_viewing_calendar_event_id AND calendar_event_viewing_type='2' WHERE (calendar_event_viewing_public_notes='Unable to
SELECT ce.*, cev.*
FROM calendar_event ce
INNER JOIN calendar_event_viewing cev
on ce.calendar_event_id = cev.calendar_event_viewing_calendar_event_id AND calendar_event_viewing_type='2'
WHERE (calendar_event_viewing_public_notes='Unable to reach viewer to obtain feedback yet, will keep trying.' OR calendar_event_viewing_public_notes='Unable to reach viewer to obtain feedback yet, voicemail left, will keep trying.')
ORDER BY calendar_event_start ASC
表
calendar\u event\u viewing
可以有多个条目。我想选择最新条目作为内部联接的一部分。对于日历\事件\查看\日历\事件\ id
,最新条目的值最高。默认情况下,如果在查看日历事件时有多行要加入,MySQL会选择哪一行?您可以在查询中执行子查询,只返回最新的日历事件查看
SELECT ce.*, cev.*
FROM calendar_event ce
INNER JOIN (SELECT *
FROM calendar_event_viewing
GROUP BY calendar_event_viewing_calendar_event_id
ORDER BY **id** DESC) cev
on ce.calendar_event_id = cev.calendar_event_viewing_calendar_event_id AND calendar_event_viewing_type='2'
WHERE (calendar_event_viewing_public_notes='Unable to reach viewer to obtain feedback yet, will keep trying.' OR calendar_event_viewing_public_notes='Unable to reach viewer to obtain feedback yet, voicemail left, will keep trying.')
ORDER BY calendar_event_start ASC
将**id**
替换为表示最新事件的任何列。表格日历事件查看可以有多个条目。最新条目的日历\u事件\u查看\u日历\u事件\u id值最高。但您正在加入日历\u事件\u查看\u日历\u事件\u id
。这对我来说毫无意义。您可能想使用另一列来标识最新条目。此外,每个日历事件\u查看\u日历事件\u事件id
,您可能在日历事件中有多条记录,