【问题标题】:MYSQL use row number in where clauseMYSQL 在 where 子句中使用行号
【发布时间】:2014-10-22 08:16:32
【问题描述】:

如何在 where 子句中使用我的行号?

假设我有 25 个数据,我只想获取前 10 行或从 11 到 20 的行。

如何使用行号?我无法在下面执行此查询

SET @row_number:=0;
SELECT @row_number:=@row_number+1 AS row_number, col1, col2, col3
FROM table
WHERE row_number > 1 and row_number < 10;

更新:问题是在我的 where 子句中 row_number 在表中是未知的 更新 2: 谢谢大家!无论如何,我找到了这个。 Select where row number = rownum

【问题讨论】:

  • 它说未知列row_number..这是为什么? D:
  • 因为它不是列!?
  • 那我如何获得行号? D:
  • 检查我的编辑@ClydeSanchez
  • 在查询中使用 rownum,同时限制选择。类似:SELECT @row_number:=@row_number+1 as row_number, col1, col2, col3 FROM table LIMIT 1, 10;

标签: mysql sql


【解决方案1】:

你需要使用OFFSETLIMIT

Check this article

所以要获得第 11 到 20 行,你会这样做

SELECT col1, col2, col3
FROM table
LIMIT 10 OFFSET 10;

【讨论】:

  • 问题是“where子句中的未知列row_number”
  • 是的,抱歉,我只是复制并粘贴了您的代码,并没有意识到所有试图实现LIMITOFFSET 的功能。检查我的编辑
  • 天哪,谢谢!我的代码有点复杂,我正在尝试这个stackoverflow.com/questions/12821709/select-where-row-number-rownum:D
猜你喜欢
  • 2010-09-23
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 2015-10-26
  • 1970-01-01
相关资源
最近更新 更多