获取 select...group by mysql 语句的行数
我有一个产品表,其中包含大约 2000 种服装产品,每个产品都有一个 grpIdent
字段和一个 productGroup
字段。
当我运行以下查询时:
select count(1)
from tblclothingitems ci
where productGroup='hel'
group by productGroup, grpIdent
根据 SQL Yog,我得到 99 行的结果集,其中包含与每个组的行数相关的不同值。但我想返回数字 99 给出返回的行数。
有人知道我如何才能实现这一目标吗?
I have a table of products which contains some 2000 clothing products, each product has a grpIdent
field and a productGroup
field.
when I run the following query:
select count(1)
from tblclothingitems ci
where productGroup='hel'
group by productGroup, grpIdent
I get a resultset of 99 rows, according to SQL Yog, containing different values pertaining to the number of rows for each group. BUT I want to return the number 99 giving the number of rows returned.
Does anybody have an idea on how I can achieve this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将返回查询返回的行数。
Will return the number of rows from your query returns.
将返回 grpIdent 不同值的计数。
第 1 点。您可以嵌套表格并选择 count(*) 来计算行数。
第 2 点。下面是值基数的首选查询。
will return counts for different values of grpIdent.
Point 1. You could nest the table and select count(*) to count the rows.
Point 2. This below is the preferred query for the cardinality of the values.
如果您想要单个值,则必须选择一个变量。
像这样:
编辑:
看来我问错了你的问题。如果我现在明白了,你可以使用:
从 tblclothingitems 中选择 COUNT(不同的产品组)
If you want a single value, you have to select into a variable.
Like this:
EDIT:
Seems i got your question wrong. If i understand it right now, you could use:
SELECT COUNT(DISTINCT productgroup) FROM tblclothingitems
使用 MySQL 的内置函数“SQL_CALC_FOUND_ROWS”,其工作原理如下,使用上面的示例查询;
然后发出第二条MySQL命令;
并在您的软件中使用该结果 - 您可以在许多领域使用它,例如更新数据,或者使用“LIMIT”子句来限制返回到应用程序的行数。
SQL_CALC_FOUND_ROWS 告诉 MySQL 计算行数,忽略“LIMIT”子句,这允许您执行类似的操作。
从第100行开始提取10行,然后;
这将告诉您整个表的
title
字段中有多少行带有“blah”,然后您可以将此信息显示为“Showing 10 rows from 12345 Total”Use the built in function of MySQL "SQL_CALC_FOUND_ROWS" which works as follows, using your example query above;
Then issue a second MySQL command;
And use that result within your software - you can use this in many areas, for example updating data, or where you are using a "LIMIT" clause to restrict the number of rows returned to your app.
The SQL_CALC_FOUND_ROWS tells MySQL to calculate the number of rows ignoring the "LIMIT" clause, which allows you to do something like.
To extract 10 rows starting from the 100th row, and then;
Which will tell you how many rows there are with "blah" in the
title
field in the whole table, you can then present this info as "Showing 10 rows from 12345 total"