2016-11-08 73 views
1

我有一个SQL查询是这样的:MySQL查询用在正则表达式的条款

"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, "','")+")""; 

谢谢。

+0

GROUP_ID是varchar数据类型的数字类型? – mhasan

+0

group_Id是int并且groupIds是varchar – rakeeee

回答

1

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

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

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

如果它预计字符串 - 你缺少出发“‘‘改变你的代码如下

如果预计整型 - 你不应该换你的逗号分隔符’’”,将其删除如下

"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, "','")+"'"+")"; 

尝试运行此查询,看看你得到想要的结果集

+0

我需要传递groupIds的正则表达式字符串列表,我无法在普通的sql select查询中进行此操作。 – rakeeee

+0

你的正则表达式字符串是怎样的? – mhasan

+0

它看起来像1.12345.2,我只对%.12345。%值的列表感兴趣。 在这里看到这个职位。 http://stackoverflow.com/questions/9540087/can-the-operator-use-like-wildcards-in-oracle – rakeeee

0

也许问题在于使用方法的STR ingUtils.join。 你可以像下面的代码一样编辑你的sql。

select f.filterid as filtename, f.id as filtertext from filter f where group_Id in ('groupA_id', 'groupB_id', 'groupC_id') 

如果您ids{"groupA_id", "groupB_id", "groupC_id"},然后

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

尝试这样:

Query query = session.Query("select f.filterid as filtename, f.id as filtertext from filter f where group_Id in :list"); 
query.SetParameterList(":list", ListOfIds);