sql直接获取表中的行数的方法 [英] sql direct way to get number of rows in table

查看:495
本文介绍了sql直接获取表中的行数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次stackoverflow的人们。
我有一个例程,该例程有一个步骤,我发现不必要的
可以说您要从图库中获取所有图像,并限制每页的图像数量。

  $ db = PDO对象
$ start =(pagenum x每页图像)
$ limit =(每页图像)
$ itemsdata = $ db-> query( SELECT id,name from gallery LIMIT $ start,$ limit)-> fetchAll();
$ numitems = $ db-> query( SELECT id FROM gallery)-> rowCount();

$ imgsdata 是所有例如画廊中的图像。
$ numimgs 是图库中图像的数量。



您需要 $ imgsdata 在数组中的每个图像上执行foreach循环,而需要
$ numimgs 来生成页面编号(例如<< 1 2 3 4 >>)



我的怨恨与 $ db-> ; query( SELECT id FROM gallery)-> rowCount();
感觉完全像是一种作弊,没有直接的方法来获取行数在表中,类似于 SELECT gallery.Rows



ps当前,我正在使用SQLite,但MySQL和PostgreSQL也需要它。

解决方案

这将告诉您行数:

 从库

Hi again people of 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. $numimgs is the number of images that the gallery has.

you would need $imgsdata to do a foreach loop on each image in the array, while $numimgs is needed to generate the page numbering (e.g. << 1 2 3 4 >>)

my grudge is with $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?

p.s. currently I'm using SQLite, but I'd need it for MySQL and PostgreSQL as well.

解决方案

This will tell you the number of rows:

SELECT COUNT(*) FROM gallery

这篇关于sql直接获取表中的行数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆