MySQL5.6.20版本
開發(fā)提了一個需要分頁的存儲過程需求,剛來時理解是,只要帶入一個變量,根據(jù)變量計算下值,就直接分頁實現(xiàn)了...可結(jié)果,創(chuàng)建存儲過程的時候報錯了...查了很多資料,后來才得知,limit后面不能帶變量.于是就想到直接吧帶入?yún)?shù)進(jìn)行計算.在吧帶入?yún)?shù)進(jìn)行分頁...
把大概過程貼一下...自己也記錄一下:
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)
當(dāng)前名稱:mysql存儲過程翻頁
網(wǎng)站URL:
http://weahome.cn/article/ghscie.html