SELECT
COUNT(*) AS TotalUserVotes
FROM tblArticleVotes AS v
INNER JOIN tblArticles AS a
ON v.ArticleID = a.ID
AND a.AuthorID = 13405
这总是返回0.我有一个表tblArticles
,其中包含AuthorID
和ID
(文章的主键)。
然后我有一个文章投票表,其中包含articleID
列。
鉴于用户13405
,我需要了解他们所有文章中的投票数量!
要确认,目前tblArticleVotes
中有一条记录5
的文章ID。文章ID 5的作者ID为13405。
答案 0 :(得分:4)
嗯...... JOIN中的AuthorID看起来很奇怪......也许这样效果更好?
SELECT COUNT(*) AS TotalUserVotes
FROM tblArticleVotes AS v INNER JOIN
tblArticles AS a ON v.ArticleID = a.ID
WHERE a.AuthorID = 13405
答案 1 :(得分:2)
尝试将“AND”更改为“WHERE”:
SELECT COUNT(*) AS TotalUserVotes
FROM tblArticleVotes AS v INNER JOIN
tblArticles AS a ON v.ArticleID = a.ID WHERE a.AuthorID = 13405
HTH
答案 2 :(得分:2)
您的查询没有任何问题。为了解决这个问题,打破查询。
DECLARE @ArticleID int
SELECT @ArticleID = a.ID FROM tblArticles a WHERE a.AuthorID=13405
PRINT @ArticleID
SELECT * FROM tblArticleVotes where ArticleID=@ArticleID
答案 3 :(得分:1)
试试这个:
SELECT a.AuthorID, COUNT(v.id) as votes
FROM tblArticles AS a
LEFT JOIN tblArticleVotes AS v ON a.ID = v.ArticleID
GROUP BY a.ID
WHERE a.AuthorID = :author_id