SQL 对多个表进行计数和连接

发布于 2024-12-20 21:19:28 字数 648 浏览 1 评论 0原文

有一个产品表,其中包含所有产品详细信息。一个产品可以被提名到热门表中,不同的人可以对其进行投票。我只需要计算注册用户的投票。目前,它正在计算特定产品的所有投票。我该如何克服这个问题?我已经将我的sql代码粘贴在下面:

SELECT popular.name, popular.product_id, product.text, product.author, count(        vote.product_id ) AS votes
FROM popular
LEFT JOIN product ON ( product.id = popular.product_id )
LEFT JOIN vote ON ( vote.product_id = popular.product_id )
where popular.hidden = '1' and
popular.name in (select registered.name from registered 
where registered.course_id='".$course_id."' and 
registered.section = '".$section."' and 
registered.term = '".$term."')
GROUP BY popular.product_id
ORDER BY votes DESC Limit 10

There is a product table which contains all the product details. A product can be nominated to the popular table from where different people can vote on it. i need to count only the votes of registered users. Currently it is counting all the votes that a particular product has. How do i overcome this problem? i have pasted my sql code below:

SELECT popular.name, popular.product_id, product.text, product.author, count(        vote.product_id ) AS votes
FROM popular
LEFT JOIN product ON ( product.id = popular.product_id )
LEFT JOIN vote ON ( vote.product_id = popular.product_id )
where popular.hidden = '1' and
popular.name in (select registered.name from registered 
where registered.course_id='".$course_id."' and 
registered.section = '".$section."' and 
registered.term = '".$term."')
GROUP BY popular.product_id
ORDER BY votes DESC Limit 10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Bonjour°[大白 2024-12-27 21:19:28

您缺少投票和其他表之间的连接

Select P.ID, P.text, count(v.Product_ID) as votes
FROM Product PRO
LEFT JOIN Popular Pop
  ON PRO.ID = Pop.Product_ID
LEFT JOIN VOTE V   --<-- This is missing
  on V.Product_ID = Pro.ID
Group by P.ID, Pro.Text
order by votes Desc

You're missing a join between votes and your other tables

Select P.ID, P.text, count(v.Product_ID) as votes
FROM Product PRO
LEFT JOIN Popular Pop
  ON PRO.ID = Pop.Product_ID
LEFT JOIN VOTE V   --<-- This is missing
  on V.Product_ID = Pro.ID
Group by P.ID, Pro.Text
order by votes Desc
梦里南柯 2024-12-27 21:19:28
SELECT pop.product_id, pd.text, count( vt.product_id ) AS votes
FROM popular pop
LEFT JOIN product pd ON(pd.product = pop.product_id)
LEFT JOIN vote vt ON(vt.product_id = pop.product_id)
WHERE 1 = 1 // or pop.product_id = yourProductId here
GROUP BY pop.product.id
ORDER BY vt.votes DESC

SELECT pop.product_id, pd.text, count( vt.product_id ) AS votes
FROM popular pop
LEFT JOIN product pd ON(pd.product = pop.product_id)
LEFT JOIN vote vt ON(vt.product_id = pop.product_id)
WHERE 1 = 1 // or pop.product_id = yourProductId here
GROUP BY pop.product.id
ORDER BY vt.votes DESC

甜宝宝 2024-12-27 21:19:28
SELECT popular.product_id, product.text, count( vote.product_id ) AS votes
FROM popular, product, vote
WHERE product.id = popular.product_id AND product.id = vote.product_id
GROUP BY product.id
ORDER BY votes DESC

请注意,此查询不会返回没有投票的产品。如果您的数据库中有此类记录,您应该LEFT JOINvote表。

SELECT popular.product_id, product.text, count( vote.product_id ) AS votes
FROM popular, product, vote
WHERE product.id = popular.product_id AND product.id = vote.product_id
GROUP BY product.id
ORDER BY votes DESC

Note that this query does not return products having no votes. If there are such records in your DB, you should LEFT JOIN with vote table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文