0

我有如下三个表,并试图提取为成员的最高访问数提供问卷的次数。这样做的目的是防止在最近两次提示访问者的情况下向访问者提示问卷(一旦经过一定时间,visit_number 字段就会增加)。我的 bash 如下(如下表),但这显然是不对的,我得到了数千个结果。希望你至少能看到我想用它做什么

Members
->id

Visits
->id
->visit_number
->member_id

Questionnaire
->id
->visit_id

SELECT COUNT(*)
FROM questionnaires q
JOIN visits v 
ON q.visit_id = (SELECT MAX(id) FROM visits v WHERE v.member_id = ".$this->id.")
ORDER BY q.id DESC
4

1 回答 1

1

尝试这个:

SELECT COUNT(*)
FROM questionnaires q
INNER JOIN Visits v1 ON q.visit_id = v1.id
INNER JOIN
(
    SELECT MAX(visit_number) MaxVisitNumber, member_id
    FROM visits
    GROUP BY member_id
) v2 ON v1.member_id = v2.member_id AND v1.visit_number = v2.MaxVisitNumber
INNER JOIN members m ON v2.member_id = m.Id
WHERE m.Id = @someId
于 2012-10-09T14:08:14.957 回答