我在网站上尝试了各种解决方案,但不幸的是我没有成功。我需要这个已经采用字段“ vote_up”的最大值的结果的查询,也可以采用第二个附加值。它在给定日期(posted_date),开始日期和结束日期之内提取结果。
SELECT *
FROM stories S
LEFT JOIN categories C
ON C.category_id = S.c_id
LEFT JOIN (SELECT voted_story,
voter_ip
FROM votes
WHERE voter_ip = '$ip_address'
GROUP BY voted_story) V
ON V.voted_story = S.story_id
WHERE S.story_active = 1
ORDER BY S.vote_up DESC
LIMIT 0, 16
为澄清起见,我遇到的困难是使我出错的语法,尤其是我不知道如何在同一查询中插入两个“ where”。
答案 0 :(得分:1)
您可以添加其他WHERE
条件条款,以便同时检查过帐日期。
SELECT *
FROM stories S
LEFT JOIN categories C
ON C.category_id = S.c_id
LEFT JOIN (SELECT voted_story, voter_ip
FROM votes
WHERE voter_ip = "$ip_address"
GROUP BY voted_story) V
ON V.voted_story = S.story_id
WHERE
S.story_active = 1 AND
S.posted_date > "$startDate" AND
S.posted_date < "$endDate"
ORDER BY S.vote_up DESC
LIMIT 0, 16