Mysql 分页查询limit 不能使用运算符

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

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

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

因为mysql中limit后面不能带运算符,只能是常量。
解决方法:
使用concat,动态sql。

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

文章转载自:https://blog.csdn.net/changhea/article/details/88670881

原创文章 6 获赞 6 访问量 312

猜你喜欢

转载自blog.csdn.net/qq_43619271/article/details/106169493