gpt4 book ai didi

java - 带有 In 子句正则表达式的 Mysql 查询

转载 作者:行者123 更新时间:2023-11-28 23:23:30 25 4
gpt4 key购买 nike

我有一个像这样的 SQL 查询:

"select f.filterid as filtename, f.id as filtertext " +
"from filter f " +
"where group_Id = '" + id +"' " +
"OR groupIds like '%." + id + ".%' ";

我想将一个 ID 列表传递给此查询以提高性能。我不知道 REGEX 是否适用于 IN 子句。我尝试了下面的方法,但它不起作用,并且不确定在 REGEX 的情况下使用什么。

"select f.filterid as filtename, f.id as filtertext from filter f " + 
"where group_Id in ("+StringUtils.join(ids, "','")+")" +
"OR groupIds in ("+StringUtils.join(ids, "','")+")"";

谢谢。

最佳答案

我建议使用 Query#setParameter 来实现这一点,如果您使用的是 JPA,则可以轻松地在 setParameter 中提供您的 ID 列表。

但对于您当前的分辨率,您可以尝试以下更改。

不确定您的 group_Id 列是否需要整数或字符串数​​据类型,我会针对这两种情况提出更改建议。

如果需要 String - 您缺少开头的“'”,请按以下方式更改您的代码

如果需要整数类型 - 你不应该用“'”包裹你的逗号分隔符,如下删除它们

"select f.filterid as filtename, f.id as filtertext from filter f " + "where group_Id in ("+StringUtils.join(ids, ",")+")" + "OR groupIds in ("+"'"+StringUtils.join(ids, "','")+"'"+")";

尝试运行此查询并查看是否获得所需的结果集

关于java - 带有 In 子句正则表达式的 Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com