如何使用CodeIgniter计算MySQL中给定行之前的行数? [英] How to count the number of rows before a given row in MySQL with CodeIgniter?

查看:31
本文介绍了如何使用CodeIgniter计算MySQL中给定行之前的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我如何计算某行之前有多少行.我使用的是增量ID,但是行是随机删除的,因此只需检查一下ID是否起作用即可.

Simply put, how can I count how many rows there are before a certain row. I'm using incremental ID's, but rows are deleted a random, so just checking to see what the ID is won't work.

例如,如果我有30行,并且我已经基于一个名称(或其他真实名称)选择了一行,那么在该行之前有几行?可以是16、1、12或其他任何东西.

If I have, say, 30 rows, and I've selected one based on a name (or anything really), how many rows are there before that one? It could be 16, 1, 12, or anything.

我正在使用MySQL和CodeIgniter.

I'm using MySQL and CodeIgniter.

推荐答案

我假设您的主键列的数据类型为整数

I assume your primary key column has datatype integer

SELECT COUNT(*) FROM `table`
WHERE id < (SELECT id FROM `table` WHERE `conditions are met for specific row`)

这篇关于如何使用CodeIgniter计算MySQL中给定行之前的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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