php 从“mysql_query”的结果中计算行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count rows from results of a "mysql_query"
提问by David
If I have this:
如果我有这个:
$results = mysql_query("SELECT * FROM table_name WHERE id=$id");
is there then any way to check how many rows which have a field-value of "Private" or "Company" ?
那么有没有办法检查有多少行的字段值为“私人”或“公司”?
I need to show the user how many "Private" and "Company" records where found, without making another query. (There is a column called 'ad_type' which contains either "private" or "company")
我需要向用户显示找到了多少“私人”和“公司”记录,而不进行其他查询。(有一个名为“ad_type”的列,其中包含“私人”或“公司”)
I already know the mysql_num_rows for counting all rows!
我已经知道用于计算所有行的 mysql_num_rows!
EDIT: There are 500thousand records! So maybe an iteration through the result is slow, what do you think?
编辑:有 500,000 条记录!所以也许结果的迭代很慢,你怎么看?
Thanks for all help :)
感谢所有帮助:)
回答by David
The above answers are great and all, but the currently checked answer will work very inefficiently should you be dealing with a large amount of data
以上答案都很好,但是如果您处理大量数据,当前检查的答案将非常低效
Example of the above answer (via Gal)
上述答案的示例(通过 Gal)
$results = mysql_query("SELECT *,(SELECT COUNT(*) FROM table_name WHERE column=$value) count FROM table_name WHERE id=$id");
It's good and all, and it returns what you need but the obvious design flaw is that making your SQL server return the results then re-return them and look at just the count is very inefficient for large amounts of data.
一切都很好,它返回你需要的东西,但明显的设计缺陷是让你的 SQL 服务器返回结果然后重新返回它们并只查看计数对于大量数据是非常低效的。
Simply do this:
只需这样做:
$results = mysql_query("SELECT * FROM table_name WHERE column=$value");
$num_rows = mysql_num_rows($result);
It will yield the same results and be much more efficient in the long run, additionally for larger amounts of data.
从长远来看,它会产生相同的结果并且效率更高,另外对于更大量的数据。
回答by Gal
You can do something like:
您可以执行以下操作:
$results = mysql_query("SELECT *,(SELECT COUNT(*) FROM table_name WHERE column=$value) count FROM table_name WHERE id=$id");
in order to fetch the number with sql.
为了用sql获取数字。
回答by Jakob Stoeck
If you don't want to change your query you could do a
如果你不想改变你的查询,你可以做一个
$results = mysql_query("SELECT * FROM table_name WHERE id=$id");
$count = mysql_num_rows($results);
回答by Eric Wang
steps to get a count():
获得 count() 的步骤:
- use mysql_query() to get count,
- use mysql_fetch_array() to get the only 1 row
- get the only one column of the row, this is the count,
- 使用 mysql_query() 获取计数,
- 使用 mysql_fetch_array() 获取唯一的 1 行
- 获取该行唯一的一列,这是计数,
here is an example, which check whether the email is already used:
这是一个示例,它检查电子邮件是否已被使用:
// check whether email used
$check_email_sql = "select count(*) from users where email='$email'";
$row = mysql_fetch_array(mysql_query($check_email_sql));
$email_count = $row[0];
回答by Alexander Gessler
Iterate through the result set of rows and count the number of occurences of Privateand Companyin ad_type, respectively?
遍历行的结果集并分别计算Private和Companyin的出现次数ad_type?
回答by itavero
I guess this query would do the job:
我想这个查询可以完成这项工作:
SELECT ad_type, count(*) FROM table_name WHERE id=$id GROUP BY ad_type;
I don't see any reason so far to use HAVING, since you probably want to show the user an overview of all the ad_type's found in DB (at least you didn't mention that there are other values for ad_type then the two given). I also strongly suggest NOT to use sub-queries; always try to use just one. If there's one thing that will slow your query down, it's a subquery (or subqueries).
到目前为止,我没有看到任何使用 HAVING 的理由,因为您可能希望向用户展示在 DB 中找到的所有 ad_type 的概览(至少您没有提到 ad_type 有其他值,然后是两个给出的值) . 我也强烈建议不要使用子查询;总是尝试只使用一个。如果有一件事会减慢您的查询速度,那就是子查询(或子查询)。
Good luck!
祝你好运!
回答by AndiDog
You can do
你可以做
SELECT COUNT(*) FROM table_name WHERE id=$id GROUP BY fieldvalue HAVING fieldvalue = "Private"
SELECT COUNT(*) FROM table_name WHERE id=$id GROUP BY fieldvalue HAVING fieldvalue = "Company"
but that would be another query. But if you process the data anyway, you could simply sum up the number of "Private" and "Company" rows after doing the query.
但这将是另一个查询。但是,如果您无论如何都处理数据,则可以在执行查询后简单地总结“私人”和“公司”行的数量。
回答by Doug
In the case you don't have to get all results, use this.
如果您不必获得所有结果,请使用它。
SELECT ad_type, COUNT(*)
FROM table_name
WHERE (id=$id)
GROUP BY ad_type
HAVING ((ad_type = 'Private') OR (ad_type = 'Company'))
If you still have to fetch all the records where id = $id, it won't work. But executing such a query (once) before fetching the real data should be more efficient than using a subquery.
如果您仍然必须在 where 中获取所有记录id = $id,则它将不起作用。但是在获取真实数据之前执行这样的查询(一次)应该比使用子查询更有效。
回答by Ignacio Vazquez-Abrams
Iterate through the results of the query and keep a count of how many of each show up in local variables.
遍历查询的结果并计算每个结果出现在局部变量中的数量。

