我有两张桌子:
带有字段的cc_videos:id,challenge_id,upload_date,owner_id
cc_video_votes,包含字段ID,video_id,vote_date
Field" id"在" cc_videos"对应一个字段" video_id" in" cc_video_votes"我有这样的陈述:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
现在这样可以正常工作 - 列出所有视频按照具有最高投票率的视频排序。现在我想条件只列出来自" cc_videos"的视频。 WHERE challenge_id匹配变量" $ challenge_id",但是当我放置这样的条件时它返回0结果:
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos WHERE challenge_id = "$challenge_id"
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
GROUP BY cc_videos.id
ORDER BY votes_count desc
我是否正确使用WHERE子句?
答案 0 :(得分:1)
不,你不是。
SELECT cc_videos.*, COUNT(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count desc
WHERE
语法应保留在子句check documentation here
答案 1 :(得分:0)
将您的查询更改为:
SELECT cc_videos.*,
Count(video_id) AS votes_count
FROM cc_videos
LEFT JOIN cc_video_votes
ON cc_videos.id = cc_video_votes.video_id
WHERE cc_videos.challenge_id = "$challenge_id"
GROUP BY cc_videos.id
ORDER BY votes_count DESC