如何从db中检索特定列等于一组字符串的记录?

时间:2013-06-20 05:09:45

标签: java mysql oracle hibernate

我有一个表T1,其中包含列ID(主键,varchar),名称(varchar),值(数字)和键(数字)。我有一个由ID组成的String数组,我想要检索所有记录在单个查询中使用表T1中的String数组中的那些id。如何在oracle中编写此查询?

我目前正在使用for循环并获取记录:

for(String id: idList)
{
   //Query to get record one by one (Select * from t1 where ID='id')
}

您能否建议我在一个查询中检索所有这些值的查询?

3 个答案:

答案 0 :(得分:1)

使用以下查询

select * from t1 where id in ('id1', 'id2', 'id3')

要将ArrayList转换为格式,请执行以下操作

StringBuilder sb = New StringBuilder();
for(String id: idList)
{
   sb.append("'"+id+"',")
}
sb.deleteCharAt(sb.length() -1);
sb.toString();

答案 1 :(得分:1)

//Prep the query string
//---------------------------------

    $strQ ="";
    $len = count($idList);

    $strQ = "'".$idList[0]."'";

    for($i=1; $i<$len; $i++){
       $strQ += " or ID = '".$idList[$i]."'";
    }

//Now Da Query
//---------------------------------------

"Select * from t1 where ID= ".$strQ;

//---------------------

我上面用PHP解释了da ...

答案 2 :(得分:0)

试试这个

List<Long> itemIDs = new ArrayList<Long>();

itemIDs.add(10); .... .... AuthService.deletePracticeAppMenu(itemIDs,selectedPractice.getID());


public void deletePracticeAppMenu(List<Long> prAppMenuIDs, Long practiceID) {
    String query = "delete from PracticeAppMenu where PrAppMenuID  in (:appMenuIDs) and practiceID = :pracid ";
    Query q = getCurrentSession().createQuery(query);
    q.setParameterList("appMenuIDs", prAppMenuIDs);
    q.setParameter("pracid", practiceID);
    q.executeUpdate();
}