开发者

Stuck in MYSQL procedure using 'order by variable'

this is paramater

IN `_user_id` VARCHAR(50), 
IN `_page` INT, 
IN `_sort_type` VARCHAR(50), 
IN `_order_type` VARCHAR(50)

and this is procedure

BEGIN

if _page < 0 then
    set _page = 0;
end if;

if _sort_type='idx' then
    set _sort_type = '__idx';
end if;


PREPARE STMT FROM
"
select idx, trash, publisher_user_id as user_id
from n_board 
where publisher_user_id = ?
order by ? desc LIMIT ?,?";
SET @publisher_user_id = _user_id;
SET @sort_type = _sort_type;
SET @order_type = _order_type;
SET @START = _page*4;
SET @END = 4;
EXECUTE STMT USING @publisher_user_id, @sort_type, @START, @END;
END

This is part of my code. I made this code due to the limit variable. I heard that MYSQL 5.5 supports code like this 'limit variable, variable'. Is it correct? I'm using 5.0.77.

I hope to use order by and sort type like this:

'order by ? ? LIMIT ?,?'

but this is does not work.

select no from n_board where id = ? order by ? ? LIMIT ?,?"; -> syntax error

select no from n_board where id = ? order by ? desc LIMIT ?,?";

-> I get results, but 'ord开发者_如何学运维er by desc' is not having any effect.

What can i do?


Try not using @LIMIT as a variable, it could be upset because its a reserved word.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜