2017-09-13 56 views
0

有一個in參數爲頁面。我想要做的就是從中排除1並乘以10,但每次都會給我一個錯誤。我無法在存儲過程中進行aritmetic操作mysql

IF !a THEN 

    SELECT * 
    from entry 
    WHERE topic_foreign_id = ( 
    select topic_id 
    from topic 
    where topic_name = topicName 
) 
    ORDER BY entry_time ASC 
    LIMIT 10 OFFSET page; 

ELSE 

    SELECT * 
    from entry 
    WHERE topic_foreign_id = ( 
    select topic_id 
    from topic 
    where topic_name = topicName 
) 
    ORDER BY entry_time ASC; 

END IF 

這個代碼行的偉大工程,但是當我想在第一個SQL查詢myAdmin的aritmetic操作拋出一個錯誤每次。

SELECT * 
from entry 
WHERE topic_foreign_id = ( 
    select topic_id 
    from topic 
    where topic_name = topicName 
) 
ORDER BY entry_time ASC 
LIMIT 10 OFFSET 10 * (page - 1); //throws error 
+0

你忘了告訴我們什麼錯誤實際上是使用該值。 –

+0

@AlexK。看第二行代碼行結束:10 *(page - 1)是錯誤原因。 –

+0

請參閱https://stackoverflow.com/questions/300697/using-variables-as-offset-in-select-statments-inside-mysqls-stored-functions –

回答

0

--- EDITED ---

LIMIT似乎並不喜歡計算。嘗試使用一個變量,在您的存儲過程

DECLARE v_page_offset INT DEFAULT 0; 
SET v_page_offset = 10*(page-1); 

再後來

LIMIT 10 OFFSET v_page_offset;