【问题标题】:How to create int list from string in mysql?如何从 mysql 中的字符串创建 int 列表?
【发布时间】:2017-03-28 21:23:16
【问题描述】:

我有一个 mysql 存储过程,它接受一个字符串,它是一个整数列表。我想使用 msql ,,in (),, 搜索这个字符串。

示例: 字符串参数 = "1,2,3,4,5,6";

select * from table where table.id in (parameter);

【问题讨论】:

    标签: mysql sql string stored-procedures int


    【解决方案1】:

    我猜你可以使用find_in_set:

    select *
    from table
    where find_in_set(id, parameter);
    

    但是,请注意,以上将限制优化器在 id 列上使用索引(如果有的话)。

    如果可以,在检查应用程序中的 SQL 注入等之后,将其连接到 SQL,这样会更好:

    "select * from table where id in (" + parameter + ")"
    

    并使用它来执行。

    【讨论】:

    • 我也完全按照您的建议使用 find_in_set 执行此操作,但它只返回一行,这可能是因为 find_in_set 行为或将字符串转换为 int,它在第一个非 int 字符处停止。直接从应用程序调用 SQL 是一种可能的解决方案。但如果可能的话,我更喜欢存储过程。
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多