0
点赞
收藏
分享

微信扫一扫

mysql中limit后面不能使用运算符

大雁f 2023-10-08 阅读 17

mysql中limit后面不能使用运算符

进行分页查询的时候,如果写成以下sql,语句执行会报错:

  1. select * from user where id = 123456 and code = 111
  2. and create_date >= 20190101 and create_date <= 20190202
  3. limit (1 - 1) * 1, 20

因为mysql中limit后面不能带运算符,只能是常量。

解决方法

使用concat,动态sql。

  1. set @sql = concat('select* from user where id= 123456 andcode= 111
  2. and create_date >= 20190101 and create_date <= 20190202 limit', (1-1)*1,',20');
  3. prepare texts from @sql;
  4. execute texts;

这种方式说白了就是先计算出数值,再通过concat函数拼接sql,再进行执行。

举报

相关推荐

0 条评论