获取 MySQL 查询中返回的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15005916/
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
Get number of rows returned in MySQL query
提问by carbide20
I'm having some trouble with a MySQL query I'm writing. I'd like to get a count of the number of rows that my query is returning without actually returning the rows and then using mysql_num_rows or the like.
我在编写 MySQL 查询时遇到了一些问题。我想计算我的查询返回的行数,而不实际返回行,然后使用 mysql_num_rows 等。
My query is as follows:
我的查询如下:
SELECT COUNT(l.product_number_language) as counts, l.id, l.product_number, l.language, l.product_number_language
FROM bs_products_languages l
LEFT JOIN bs_products p ON (l.product_number_language = p.product_number)
WHERE l.product_number = 'C4164'
AND l.active='Y'
AND p.active='Y'
GROUP BY l.language
The following is what is being returned:
以下是返回的内容:
And what I really want is simply a count of these rows, so in this case 3.
而我真正想要的只是这些行的计数,所以在这种情况下是 3。
回答by Andy Refuerzo
Select count(*)
From
(
SELECT COUNT(l.product_number_language) as counts, l.id, l.product_number,
l.language, l.product_number_language
FROM bs_products_languages l
LEFT JOIN bs_products p ON (l.product_number_language = p.product_number)
WHERE l.product_number = 'C4164'
AND l.active='Y'
AND p.active='Y'
GROUP BY l.language
) as t