【问题标题】:How to retrieve records from db where a particular column is equal to a set of Strings?如何从数据库中检索特定列等于一组字符串的记录?
【发布时间】:2013-06-16 20:11:23
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java mysql oracle hibernate


    【解决方案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();
    

    【讨论】:

    • 问题是所有的id都在String的arraylist中。我如何将它放在表单中('id1','id2','id3')?
    【解决方案2】:
    //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 解释过...

    【讨论】:

    • 能否将 cmets 添加到您的代码中,这样我就可以理解逻辑,因为我不懂 PHP,而且我目前正在使用 Java,这真的很有帮助
    【解决方案3】:

    试试这个

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多