mysql select 查询不适用于通过用户变量设置的限制和偏移量
mysql select query not working with limit and offset set via user variables
我会简短一点:
正在工作
SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = p.category_id
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY @asc_or_desc
limit 5 offset 6;
不工作
set @asc_or_desc = 'id desc ';
set @limit_number = 5;
set @offset_number = 6;
SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = p.category_id
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY @asc_or_desc
limit @limit_number offset @offset_number;
所以,我需要 'not working' 版本才能工作。我怎样才能做到这一点?
我有同样的错误:'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 '@limit_number offset @offset_number; END''
。
为什么?类型没问题,你看...有什么问题?也许值被一些错误替代了?
我正在使用 JetBrains DataGrip,MySQL 8.0.15。
使用prepared statements解决了问题。
代码:
set @statement =
concat('SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = ?
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY id ',@asc_or_desc,'
limit ? offset ?');
prepare prepared from @statement;
EXECUTE prepared USING @category_id, @limit_number, @offset_number;
DEALLOCATE PREPARE prepared;
我会简短一点:
正在工作
SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = p.category_id
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY @asc_or_desc
limit 5 offset 6;
不工作
set @asc_or_desc = 'id desc ';
set @limit_number = 5;
set @offset_number = 6;
SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = p.category_id
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY @asc_or_desc
limit @limit_number offset @offset_number;
所以,我需要 'not working' 版本才能工作。我怎样才能做到这一点?
我有同样的错误:'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 '@limit_number offset @offset_number; END''
。
为什么?类型没问题,你看...有什么问题?也许值被一些错误替代了?
我正在使用 JetBrains DataGrip,MySQL 8.0.15。
使用prepared statements解决了问题。 代码:
set @statement =
concat('SELECT p.id,
p.name,
cat.name `category`,
prod.name `producer`,
p.images,
p.price,
p.flag_avaliable,
p.amount,
p.description,
p.options
FROM product p
INNER JOIN product_category cat ON cat.id = ?
INNER JOIN product_producer prod ON prod.id = p.producer_id
ORDER BY id ',@asc_or_desc,'
limit ? offset ?');
prepare prepared from @statement;
EXECUTE prepared USING @category_id, @limit_number, @offset_number;
DEALLOCATE PREPARE prepared;