MySQL选择查询不适用于通过用户变量设置的限制和偏移量

时间:2019-05-04 18:46:58

标签: mysql sql select stored-procedures 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;

因此,我需要“不工作”版本才能工作。我怎样才能做到这一点?

我有相同的错误:'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''

为什么?类型没问题,您会看到...什么问题?也许值可以替代某些bug?

我正在使用JetBrains DataGrip,MySQL 8.0.15。

1 个答案:

答案 0 :(得分:1)

使用准备好的语句已解决了该问题。 代码:


class _const:
    class headers:
        X_Auth_Token = "X-Auth-Token"
        X_Username = 'X-Username'
        X_Password = 'X-Password'

    class params:
        ID = "DocumentID"
        Symbols = "Symbols"
        ACRICode = "ACRICode"rketSegmentID"
        entries = "entries"
        depth = "depth"
        date = "date"
        meta = "meta"
        timestamp = "timestamp"
        message = "message"
        # ...