[英]sql direct way to get number of rows in table
Hi again people of stackoverflow. 再次嗨,stackoverflow的人们。 I have a routine that has a step that I find unnecessary lets say you want to get all the images from a gallery, and limit a certain number of images per page.
我有一个例程,该例程具有一个不必要的步骤,可以说您要从图库中获取所有图像,并限制每页的图像数量。
$db = PDO object
$start = (pagenum x images per page)
$limit = (images per page)
$itemsdata = $db->query("SELECT id,name FROM gallery LIMIT $start,$limit")->fetchAll();
$numitems = $db->query("SELECT id FROM gallery")->rowCount();
$imgsdata
is a array of all the images in a gallery for example. $imgsdata
是画廊中所有图像的数组。 $numimgs
is the number of images that the gallery has. $numimgs
是图库具有的图像数。
you would need $imgsdata
to do a foreach loop on each image in the array, while $numimgs
is needed to generate the page numbering (eg << 1 2 3 4 >>) 您将需要
$imgsdata
在数组中的每个图像上执行foreach循环,而需要$numimgs
来生成页面编号(例如<< << 1 2 3 4 >>)
my grudge is with $db->query("SELECT id FROM gallery")->rowCount();
我的不满是
$db->query("SELECT id FROM gallery")->rowCount();
It feels completely like some sort of cheat, isn't there a direct way to get the number of rows in a table, something like SELECT gallery.Rows
? 感觉完全像是一种作弊,没有直接的方法来获取表中的行数,例如
SELECT gallery.Rows
吗?
ps currently I'm using SQLite, but I'd need it for MySQL and PostgreSQL as well. ps当前我正在使用SQLite,但是MySQL和PostgreSQL也需要它。
这将告诉您行数:
SELECT COUNT(*) FROM gallery
一个简单的count()聚合函数将快速返回行数
Select count(*) from table
select count(*) from gallery
Me too! 我也是!
SELECT COUNT(*) FROM gallery
Yes, this should work the same just fine in MySQL, SQLite, and PostgreSQL. 是的,这在MySQL,SQLite和PostgreSQL中应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.