mysql - SQL限制组数

标签 mysql sql

我正在尝试创建一个简单的分页,但它似乎无法限制结果的数量。

SELECT * FROM visits GROUP by clientID ORDER BY 'date' LIMIT $from, $to

我只想获得每个客户的第一次访问(按时间顺序)并对结果进行分页。

如果我使用 $from = 6, $to = 12 进行查询,它会返回 8 行而不是 7 行。

我做错了什么?

最佳答案

来自 LIMIT 上的 MySQL 文档子句

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).

当你选择$from为6,$to为12;您不是从 6 到 12 中选择;您将选择 12 行,从 $from + 1 = 7 开始。

关于mysql - SQL限制组数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15076850/

相关文章:

php - 查看许多状态或创建一个表来存储所有状态

python - Query.all() 到 pandas 数据帧或没有 for 循环的列表

mysql - 为什么服务器上的 my.cnf 文件不完整或条目很少?

mysql - SQL 聚合未执行

c# - GroupBy 从 IEnumerable 对象列表中删除重复项

php - MySql 函数和 Mysql 函数内的 session /用户变量

sql - 每组值的自定义序列号/自动增量

javascript - 我的数据库表修改后没有更新

php - 用php显示多个搜索结果

c# - 如何在连接第三个表时对列表进行外连接