I wrote this query but gives me an error at line 15 (limit clause). we have 3 tables, state, email, and receiver.
CREATE PROCEDURE `inbox`(IN `page_size` INT,IN `page_number` INT) BEGIN set @curr = ( SELECT entered_accounts.username FROM entered_accounts WHERE entered_accounts.time = (SELECT Max(entered_accounts.time) FROM entered_accounts)); SET @tmp = page_number-1; SET @offset = page_size * @tmp; SELECT email.subject, email.body, email.time, state.readed, state.deleted FROM email INNER JOIN reciever USING(email_id) INNER JOIN state USING (email_id) WHERE state.username=reciever.username AND LOWER(reciever.username)=LOWER(@curr) AND NOT reciever.username=email.username ORDER BY email.time DESC LIMIT @offset,page_size; END
Advertisement
Answer
MySQL does not support user variables in the LIMIT
clause. Instead, you can use a regular variable:
DELIMITER // CREATE PROCEDURE `inbox`(IN `page_size` INT,IN `page_number` INT) BEGIN DECLARE v_offset INT; SET @curr = ( SELECT entered_accounts.username FROM entered_accounts WHERE entered_accounts.time = (SELECT Max(entered_accounts.time) FROM entered_accounts)); SET @tmp = page_number-1; SET v_offset = page_size * @tmp; SELECT email.subject, email.body, email.time, state.readed, state.deleted FROM email INNER JOIN reciever USING(email_id) INNER JOIN state USING (email_id) WHERE state.username=reciever.username AND LOWER(reciever.username)=LOWER(@curr) AND NOT reciever.username=email.username ORDER BY email.time DESC LIMIT v_offset, page_size; END//