简体   繁体   English

sql直接获取表中的行数的方法

[英]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.

相关问题 如何在不使用 COUNT(*) 的情况下获取 SQL 临时表中的行数 - How to get the number of rows in a SQL temporary table without using COUNT(*) 如何获取SQL中的行数? - How to get the number of rows in SQL? SQL连接具有不同行数的两个表从一个表中获取所有行 - SQL Join two tables with different number of rows get all rows from one table 是否可以获得多组行,每组包含有限数量的行,由 Django/SQL 中的表中的列过滤? - Is it possible to get multiple sets of rows, each containing a limited number of rows, filtered by a column in a table in Django/SQL? SQL:需要从一个表中获取MAX值,然后除以另一表中的总行数 - SQL: Need to get MAX values from one table and divide by the total number of rows in another table T-SQL-有没有一种方法可以根据表中指定值的行数动态输出列? - T-SQL - Is there a way that I can dynamically output columns based on the number of rows for a specified value in a table? sql server:估计的行数是关闭的 - sql server: Estimated number of rows is way off 是SQL插入的性能取决于表中的行数 - is performance of sql insert dependent on number of rows in table 为表中的所有SQL行减去相同的数字 - Subtract same number for all SQL rows in table SQL-表中数字匹配行的多个计数 - SQL - multiple counts for number matching rows in a table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM