使用“按变量排序”陷入 MYSQL 过程中
这是参数
IN `_user_id` VARCHAR(50),
IN `_page` INT,
IN `_sort_type` VARCHAR(50),
IN `_order_type` VARCHAR(50)
,这是过程
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
这是我的代码的一部分。由于限制变量,我编写了这段代码。 我听说MYSQL 5.5支持这样的代码“限制变量,变量”。正确吗?我用的是5.0.77。
我希望使用这样的 order by 和 sort 类型:
'order by ? ? LIMIT ?,?'
但这不起作用。
从 n_board 中选择 no,其中 id = ?订购依据? ? LIMIT ?,?";
-> 语法错误
select no from n_board where id = ? order by ? desc LIMIT ?,?";
-> 我得到结果,但“order by desc”没有任何效果。
我该怎么办?
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 'order by desc' is not having any effect.
What can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试不要使用 @LIMIT 作为变量,它可能会因为它是保留字而感到不安。
Try not using @LIMIT as a variable, it could be upset because its a reserved word.