选择每个第N行不起作用

时间:2013-07-22 12:39:33

标签: mysql sqlfiddle

我在my SQLFiddle中开发了一个SQL命令。 MySQL服务器设置为版本5.1.61,因为我的服务器运行5.5.31-0 + wheezy1。我将Schema复制到我的服务器并创建了一个执行代码的PHP脚本(在here的帮助下)。但是我的服务器返回了这个:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM (SELECT (@x:=@x+1) AS x, `ID` FROM realvalues) t WHERE x MOD 3 =' at line 2

SQL命令:

SET @x := 0;
SELECT *
FROM (SELECT (@x:=@x+1) AS x, `ID` FROM realvalues) t
WHERE x MOD 3 = 0;

为什么它在SQLFiddle上工作并且在我的服务器上不起作用?

1 个答案:

答案 0 :(得分:1)

select * from 
(
  SELECT id, 
         @x := @x + 1 as rank
  FROM realvalues, (SELECT @x := 0) t
) a
where rank mod 3 = 0

SQLFiddle demo