简体   繁体   中英

How to pass the multiple search patterns to sql statement using WHERE IN clause

Am having the table with below data,

  +----------+---------+
  | Country  | Product |
  +----------+---------+
  | Poland   | Lyca    |
  | USA      | Lyca    |
  | UK       | GT      |
  | Spain    | GT      |
  | Swiss    | Lyca    |
  | Portugal | GT      |
  +----------+---------+

From the above table, I am trying to fetch the data using the query which is given below,

 Select Country,Product from tab where Country in ('%pai%','%U%')

Query was executing but i am getting the empty resuls. So, kindly confirm me, whether the above query is valid or not.

Use like and or :

Select Country,Product
from tab
where Country like '%pai%' or
      Country like '%U%';

Get your patterns into a separate table:

PATTERNTABLE
SessionKey     Pattern  
----------     -------
abc123         %pai%
abc123         %U%

and then join to it:

SELECT Country, Product
FROM Tab t
INNER JOIN PatternTable pt ON pt.SessionKey= @SessionKey 
     and t.Country LIKE pt.Pattern

The trick is in how that pattern table is populated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM