本篇内容主要讲解“MySQL中怎么实现rownum功能类似的语句”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL中怎么实现rownum功能类似的语句”吧!
MySQL 如何实现 Oracle 的 ROWNUM2010/01/28 23:56> SELECT * FROM frutas;
+-----------+----------+
| nombre | color |
+-----------+----------+
| fresa | rojo |
| platano | amarillo |
| manzana | verde |
| uva | verde |
| pera | verde |
| mandarina | naranja |
| melocoton | marron |
| limon | amarillo |
+-----------+----------+
8 rows in set (0,00 sec)
【一条sql语句完成,不set变量】
SELECT @rownum:=@rownum+1 AS rownum, frutas.*
FROM (SELECT @rownum:=0) r, frutas;
mysql> SELECT @rownum:=@rownum+1 AS rownum, frutas.*
-> FROM (SELECT @rownum:=0) r, frutas;
+--------+-----------+----------+
| rownum | nombre | color |
+--------+-----------+----------+
| 1 | fresa | rojo |
| 2 | platano | amarillo |
| 3 | manzana | verde |
| 4 | uva | verde |
| 5 | pera | verde |
| 6 | mandarina | naranja |
| 7 | melocoton | marron |
| 8 | limon | amarillo |
+--------+-----------+----------+
8 rows in set (0,00 sec)
但是如何把这个查询放在 create view 里面
mysql> CREATE VIEW vw_frutas AS SELECT @rownum:=@rownum+1 AS rownum, frutas.*
-> FROM (SELECT @rownum:=0) r, frutas;
ERROR 1351 (HY000): View's SELECT contains a variable or parameter
到此,相信大家对“MySQL中怎么实现rownum功能类似的语句”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!