mysql limit with variable

梦想与她 提交于 2020-08-09 14:54:53

问题


i have error in my syntax :

SET @start := 0;
SELECT (ROUND((count(item))/2)) FROM car INTO @until; 
SELECT * from car limit @until OFFSET @start;

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 '@until OFFSET @start' at line 1

anyone can help me? thanks


回答1:


You cannot use a user-assigned variable (@until) in the LIMIT clause.

A possible solution (a variation on this):

SELECT (ROUND((count(item))/2)) FROM car INTO @until;
SELECT * FROM (
  SELECT *, 
         @rownum := @rownum + 1 AS rank
    FROM car, 
         (SELECT @rownum := 0) r
) d WHERE rank < @until;

Only downside is you lose the offset, but you can accommodate that by adjusting the WHERE clause. Otherwise, you can use a stored procedure.



来源:https://stackoverflow.com/questions/26033593/mysql-limit-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!