查询中的 SQL 计数查询
我有两个正在使用的表,一个依赖于另一个。我想获取独立表的信息并统计每行数据被依赖表使用的次数。在我当前的查询中,当我知道实际上总共有 38 条记录时,我只得到 1 条记录。我做错了什么?
SELECT r.rid, r.name, COUNT(b.bid) AS brewtot
FROM recipes r, brews b
WHERE r.uid = '#cookie.id#' AND b.rid = r.rid
I have two tables that I'm working with One dependent on the other. And I would like to get the information of the independent tables and count the number of times each row of data was used by the dependent table. With My current query I only get a record count of 1 when I know there in fact 38 records total. What am I doing wrong?
SELECT r.rid, r.name, COUNT(b.bid) AS brewtot
FROM recipes r, brews b
WHERE r.uid = '#cookie.id#' AND b.rid = r.rid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑你想要做的是添加一个
GROUP BY b.rid
假设你的问题是“每个配方有多少种啤酒”。
另外,当配方没有酿造时,您可能还想使用 LEFT JOIN 来计数 0 行:
I suspect what you want to do is add a
GROUP BY b.rid
assuming your question is "How many brews are there for each recipe."
Also you might want to use a
LEFT JOIN
to also count 0 rows when there is no brew to a recipe:那应该有效
That should work
在查询末尾添加“GROUP BY r.rid”。
Add 'GROUP BY r.rid' at the end of your query.
你必须在rid上进行group by。
您也想查看所有酿造记录吗?
You have to do group by on rid.
Do you want to also see all brews record too?.
我很惊讶这个查询甚至在没有分组依据的情况下也能工作?尝试:
在使用我的测试设置的 SQL Server 上,这会返回正确的计数。
I'm surprised the query even works without a group by? Try:
On SQL Server with my test setup, this returns the correct count.