mysql存储过程翻页
短信预约 -IT技能 免费直播动态提醒
mysql5.6.20版本
开发提了一个需要分页的存储过程需求,刚来时理解是,只要带入一个变量,根据变量计算下值,就直接分页实现了...可结果,创建存储过程的时候报错了...查了很多资料,后来才得知,limit后面不能带变量.于是就想到直接吧带入参数进行计算.在吧带入参数进行分页...
把大概过程贴一下...自己也记录一下:
mysql>delimiter //
mysql> create procedure t2 ( d1 int)
-> begin
-> set @a=(d1-1)*10;
-> select * from t1 limt @a,1;
-> end//
ERROR 1064 (42000): 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 '@a,1;
end' at line 4
mysql>create procedure t2 ( d1 int)
->begin
->set d1=(d1-1)*10;
->select * from t1 limit d1,1;
->end//
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t1;//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 1 | 001 | jak |
| 2 | NULL | mak |
| 3 | dd | kk |
| 4 | 2d | dkk |
| 5 | 2ddw2 | 9jd2 |
| 6 | 2ddw2 | 9jd2 |
| 7 | 2ddw2 | 9jd2 |
| 8 | 2ddw2 | 9jd2 |
| 9 | 2ddw2 | 9jd2 |
| 10 | 2ddw2 | 9jd2 |
+------+-------+------+
10 rows in set (0.00 sec)
mysql> call t2(3);//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 5 | 2ddw2 | 9jd2 |
| 6 | 2ddw2 | 9jd2 |
+------+-------+------+
2 rows in set (0.00 sec)
mysql> select * from t1 limit 3,2;//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 4 | 2d | dkk |
| 5 | 2ddw2 | 9jd2 |
+------+-------+------+
2 rows in set (0.00 sec)
开发提了一个需要分页的存储过程需求,刚来时理解是,只要带入一个变量,根据变量计算下值,就直接分页实现了...可结果,创建存储过程的时候报错了...查了很多资料,后来才得知,limit后面不能带变量.于是就想到直接吧带入参数进行计算.在吧带入参数进行分页...
把大概过程贴一下...自己也记录一下:
mysql>delimiter //
mysql> create procedure t2 ( d1 int)
-> begin
-> set @a=(d1-1)*10;
-> select * from t1 limt @a,1;
-> end//
ERROR 1064 (42000): 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 '@a,1;
end' at line 4
mysql>create procedure t2 ( d1 int)
->begin
->set d1=(d1-1)*10;
->select * from t1 limit d1,1;
->end//
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t1;//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 1 | 001 | jak |
| 2 | NULL | mak |
| 3 | dd | kk |
| 4 | 2d | dkk |
| 5 | 2ddw2 | 9jd2 |
| 6 | 2ddw2 | 9jd2 |
| 7 | 2ddw2 | 9jd2 |
| 8 | 2ddw2 | 9jd2 |
| 9 | 2ddw2 | 9jd2 |
| 10 | 2ddw2 | 9jd2 |
+------+-------+------+
10 rows in set (0.00 sec)
mysql> call t2(3);//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 5 | 2ddw2 | 9jd2 |
| 6 | 2ddw2 | 9jd2 |
+------+-------+------+
2 rows in set (0.00 sec)
mysql> select * from t1 limit 3,2;//
+------+-------+------+
| id | cart | name |
+------+-------+------+
| 4 | 2d | dkk |
| 5 | 2ddw2 | 9jd2 |
+------+-------+------+
2 rows in set (0.00 sec)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341