Mysql SELECT COUNT(*) 还是 SELECT 1?磷酸二氢钾
众所周知,PDO 不支持 COUNT(*) 并且像下面这样的查询会失败,因为它不返回任何受影响的行,
$q = $dbc -> prepare("SELECT COUNT(*) FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();
经过一些研究,我发现您还可以使用其他计数方法来获取行数,根本不使用计数,例如以下查询应该与上面相同,但会返回正确的 PDO,
$q = $dbc -> prepare("SELECT 1 FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();
互联网上有各种来源声称;
"SELECT COUNT(*)
"SELECT COUNT(col)
"SELECT 1
彼此都是相同的(有一些差异)那么为什么使用 mysql 的 PDO 不能正确返回真实的计数,是否
"SELECT 1
有效?
计数方法讨论
It has long been known that PDO does not support COUNT(*) and a query like below would fail as it doesn't return any affected rows,
$q = $dbc -> prepare("SELECT COUNT(*) FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();
Doing some research I found that you can also get the row count using other methods of count and not using count at all, for example the following query is supposed be the same as above but will return correct for PDO,
$q = $dbc -> prepare("SELECT 1 FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();
There are various sources on the internet claiming that;
"SELECT COUNT(*)
"SELECT COUNT(col)
"SELECT 1
Are all the same as each other (with a few differences) so how come using mysql which PDO cannot properly return a true count, does
"SELECT 1
work?
Methods of count discussion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WTF?当然PDO支持
COUNT(*)
,你使用它的方式是错误的。如果您使用的是 MySQL 以外的驱动程序,则可能必须首先测试
rowCount
,如下所示。WTF? Of course PDO supports
COUNT(*)
, you are using it the wrong way.If you are using a driver other than MySQL, you might have to test
rowCount
first, like this.哦。你把一切都搞混了。
COUNT(*)
时,您根本不应该使用 rowcount,因为它没有任何意义。您必须检索查询结果。COUNT(*)
和COUNT(col)
(甚至COUNT(1)
)是当您本身不需要记录时,这是相同且获取记录计数的唯一正确方法。COUNT 是一个聚合函数,它为您计算行数。因此,它已经返回结果,不再需要计数。它仅返回单行中的标量值。因此,在这一行上使用
rowcount
没有任何意义SELECT 1
与上面不同,因为它只选择文字1
对于表中找到的每一行。因此,如果数据库中有数千行,它将返回一千个1
。因此,行计数将为您提供结果,但这将极大地浪费服务器资源。有一个简单的规则需要遵循:
始终只请求您需要的数据。
如果您需要行数 - 请求行数。没有一千个 1 供以后数它们。
听起来有道理吗?
Oh. You are confusing everything.
COUNT(*)
you shouldn't use rowcount at all, as it just makes no sense. You have to retreive the query result instead.COUNT(*)
andCOUNT(col)
(and evenCOUNT(1)
) are the same and the only proper way to get count of records when you need no records themselves.COUNT is an aggregate function, it counts rows for you. So, it returns the result already, no more counting required. Ad it returns just a scalar value in the single row. Thus, using
rowcount
on this single row makes no senseSELECT 1
is not the same as above, as it selects just literal1
for the every row found in the table. So, it will return a thousand1s
if there is a thousands rows in your database. So, rowcount will give you the result but it is going to be an extreme waste of the server resources.there is a simple rule to follow:
Always request the only data you need.
If you need the count of rows - request count of rows. Not a thousand of 1s to count them later.
Sounds sensible?
我认为测试数据库中是否存在一行的最佳方法是执行。
如果它找到一行,它会停下来并告诉您有一行。如果没有,并且您的 where 子句列上有索引,它也会非常快地看到没有可用的索引。
Best way I think to test if a line exist in your database is to perform.
If it find a row it will stop and tell you there is a line. If it don't and you have an index on your where clause column it will also goes very fast to see there are none available.