我必须显示两名候选人之间的会议清单,其中我必须显示两名候选人之间最近一次会议的日期,时间以及他们之间安排的所有会议的次数。
查询我已写信选择候选人之间的会议:
counts = 1
给出以下输出:
SELECT cm.id AS meeting_id, cm.meeting_date, cm.meeting_time, cm.candidate1_id, cm.candidate2_id
FROM candidate_meetings AS cm
JOIN view_candidate_details AS candidate_1 ON candidate_1.id = cm.candidate1_candidate_id
JOIN view_candidate_details AS candidate_2 ON candidate_2.id = cm.candidate2_candidate_id
WHERE (candidate_1.counselor_member_id = 41 OR candidate_2.counselor_member_id = 41)
ORDER BY cm.id DESC
现在要计算两个候选人之间最近一次会议的详细情况,我将其添加到查询中:
+------------+--------------+--------------+------------------+-----------------+
| meeting_id | meeting_date | meeting_time | candidate1_id | candidate2_id |
+------------+--------------+--------------+------------------+-----------------+
| 6 | 2016-08-31 | 17:45:00 | 24 | 28 |
| 5 | 2016-08-31 | 17:30:00 | 24 | 28 |
| 4 | 2016-08-31 | 19:30:00 | 24 | 23 |
| 3 | 2016-08-31 | 18:30:00 | 24 | 22 |
| 2 | 2016-08-31 | 19:15:00 | 24 | 21 |
| 1 | 2016-08-31 | 17:15:00 | 24 | 21 |
+------------+--------------+--------------+------------------+-----------------+
运行此查询后,我得到了这个结果:
SELECT *, count(*) AS meeting_count
FROM
(
SELECT cm.id AS meeting_id, cm.meeting_date, cm.meeting_time, cm.candidate1_id, cm.candidate2_id
FROM candidate_meetings AS cm
JOIN view_candidate_details AS candidate_1 ON candidate_1.id = cm.candidate1_candidate_id
JOIN view_candidate_details AS candidate_2 ON candidate_2.id = cm.candidate2_candidate_id
WHERE (candidate_1.counselor_member_id = 41 OR candidate_2.counselor_member_id = 41)
ORDER BY cm.id DESC
) as sub
GROUP BY sub.candidate1_id, sub.candidate2_id;
但预期的结果是
+------------+--------------+--------------+------------------+-----------------+---------------+
| meeting_id | meeting_date | meeting_time | candidate1_id | candidate2_id | meeting_count |
+------------+--------------+--------------+------------------+-----------------+---------------+
| 1 | 2016-08-31 | 17:15:00 | 24 | 21 | 2 |
| 3 | 2016-08-31 | 18:30:00 | 24 | 22 | 1 |
| 4 | 2016-08-31 | 19:30:00 | 24 | 23 | 1 |
| 5 | 2016-08-31 | 17:30:00 | 24 | 28 | 2 |
+------------+--------------+--------------+------------------+-----------------+---------------+
PS:请忽略预期结果中id列的顺序。
答案 0 :(得分:1)
实际输出与预期输出之间存在差异的原因是您没有告诉MySQL您想要最新的会议日期。您的查询是针对sql标准的,因为您在选择列表中的字段既不在分组列表中,也不是max()
等聚合函数的主题。
MySQL在某些sql模式设置下允许运行此类查询,但value of such fields is indeterminate。
最好的解决方案是明确告诉MySQL你需要什么:
SELECT max(concat(cm.meeting_date, cm.meeting_time)) as latest_meeting_time, cm.candidate1_id, cm.candidate2_id
FROM candidate_meetings AS cm
JOIN view_candidate_details AS candidate_1 ON candidate_1.id = cm.candidate1_candidate_id
JOIN view_candidate_details AS candidate_2 ON candidate_2.id = cm.candidate2_candidate_id
WHERE (candidate_1.counselor_member_id = 41 OR candidate_2.counselor_member_id = 41)
GROUP BY cm.candidate1_id, cm.candidate2_id
如果您想要来自最新会议的candidate_meetings
表中的其他字段值,请访问:记录,然后将上述查询放在子查询中,并将其连接到candidate_meetings
连接条件的外部查询中的concat(candidate_meetings.meeting_date, candidate_meetings.meeting_time)=subquery_alias.latest_meeting_time
表。
答案 1 :(得分:-1)
试试这个:
SELECT *, count(*) AS meeting_count
FROM
(SELECT cm.id AS meeting_id, cm.meeting_date, cm.meeting_time, cm.candidate1_id, cm.candidate2_id
FROM candidate_meetings AS cm
JOIN view_candidate_details AS candidate_1 ON candidate_1.id = cm.candidate1_candidate_id
JOIN view_candidate_details AS candidate_2 ON candidate_2.id = cm.candidate2_candidate_id
WHERE (candidate_1.counselor_member_id = 41 OR candidate_2.counselor_member_id = 41)
ORDER BY cm.id
) as sub
GROUP BY sub.candidate1_id, sub.candidate2_id
ORDER BY cm.id DESC
您得到的预期结果是错误的,因为当您使用GROUP BY子句时,数据库会选择最后一次出现的meeting_id,meeting_date,meeting_time。