在不使用OR构造的情况下搜索多列中的字符串

时间:2016-06-15 11:56:43

标签: sql oracle

我想知道是否有更好的选择,可以在不使用or-construction的情况下在多个列中搜索字符串。 这是我的代码:

 SELECT 
   originaltktid,
   securityidentifier,
   ticker,
   customeraccountcounterparty,
   accountcounterpartyshortname,
   lastlogin,
   customertype,
   securityidentifier || ' ' || ticker || ' ' ||customeraccountcounterparty || ' ' ||accountcounterpartyshortname || ' ' ||lastlogin || ' ' ||customertype as SearchinString
 FROM bondtrades
 WHERE 1=1
 /* first option */
   AND securityidentifier || ' ' || ticker || ' ' ||customeraccountcounterparty || ' ' ||accountcounterpartyshortname || ' ' ||lastlogin || ' ' ||customertype like '%RAU%'
 /*second option
  * AND (securityidentifier like '%RAU%' 
  *  OR ticker like '%RAU%' 
  *  OR customeraccountcounterparty like '%RAU%'
  *  OR accountcounterpartyshortname like '%RAU%' 
  *  OR lastlogin '%RAU%'
  *  OR customertype like '%RAU%'
  */;

任何想法都很开心。非常感谢你。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用REGEXP_LIKE函数或CASE语句。在我的测试中,当我使用SQL开发人员"解释计划"时,REGEXP_LIKE似乎更有效。选项使用我自己的测试数据。

SELECT 
   originaltktid,
   securityidentifier,
   ticker,
   customeraccountcounterparty,
   accountcounterpartyshortname,
   lastlogin,
   customertype,
   securityidentifier || ' ' || ticker || ' ' ||customeraccountcounterparty || ' ' ||accountcounterpartyshortname || ' ' ||lastlogin || ' ' ||customertype as SearchinString
 FROM bondtrades
 WHERE 1=1
  /* Thrid option */
   AND REGEXP_LIKE(SECURITYIDENTIFIER || TICKER || CUSTOMERACCOUNTCOUNTERPARTY ||ACCOUNTCOUNTERPARTYSHORTNAME ||LASTLOGIN || CUSTOMERTYPE,'RAU');
 /* Fourth option   
    AND CASE WHEN SECURITYIDENTIFIER LIKE '%RAU%' THEN 1
        WHEN ticker LIKE '%RAU%' THEN 1
        WHEN customeraccountcounterparty LIKE '%RAU%' THEN 1
        WHEN accountcounterpartyshortname like '%RAU%' then 1
        WHEN lastlogin like '%RAU%' then 1
        WHEN customertype like '%RAU%' then 1
        ELSE 0
        end = 1
        /*;
相关问题