从"mysql_query"查询结果中计算行数 [英] Count rows from results of a "mysql_query"
问题描述
如果我有这个:
$results = mysql_query("SELECT * FROM table_name WHERE id=$id");
那么有什么方法可以检查有多少行的字段值为"Private"或"Company"?
is there then any way to check how many rows which have a field-value of "Private" or "Company" ?
我需要向用户显示找到的私人"和公司"记录数量,而无需进行其他查询. (有一个名为"ad_type"的列,其中包含私人"或公司")
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")
我已经知道用于计数所有行的mysql_num_rows!
I already know the mysql_num_rows for counting all rows!
有五十万条记录!因此,也许结果迭代很慢,您认为呢?
There are 500thousand records! So maybe an iteration through the result is slow, what do you think?
感谢所有帮助:)
推荐答案
上面的答案非常有用,但是如果您要处理大量数据,当前检查的答案将非常无效.
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
上述答案的示例(通过Gal)
Example of the above answer (via Gal)
$results = mysql_query("SELECT *,(SELECT COUNT(*) FROM table_name WHERE column=$value) count FROM table_name WHERE id=$id");
这一切都很好,它可以返回您需要的内容,但是明显的设计缺陷是,使您的SQL Server返回结果然后再返回它们,然后仅查看计数对于大量数据而言是非常低效的.
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.
只需执行以下操作:
$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.
这篇关于从"mysql_query"查询结果中计算行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!