SQL Select语句LIKE IN

时间:2014-12-22 12:18:43

标签: sql

我需要编写一个select语句,它返回一个用户列表,其中SenderSubID列表存在于名为constraint_values的字段中:

理想情况下我需要一个LIKE IN语句:

SELECT SenderSubID FROM fix_user_subids WHERE SenderSubID IN ('**00390529MGERAN1**','**00912220PBALDIS**','**03994113LDAMBRO**','**04004308SLOMBAR**','**04935278CARELLI**','**4004308SLOMBARD**')

SELECT * FROM fix_dyno_rule_defs WHERE constraint_values LIKE '%**00390529MGERAN1**%'

返回:

rule_def_id tag msg_type required constraint_values constraint_type data_type default_value validation_type trans_type attribute_tag trans_tag memo 99800 10000 D,F,G 0 ((50,4,1,00390529MGERAN1)or(50,4,1,00912220PBALDIS)or(50,4,1,03994113LDAMBRO)or(50,4,1,04004308SLOMBAR)or(50,4,1,04935278CARELLI)or(50,4,1,4004308SLOMBARD))and(21,4,1,3)#STROP1#addattr(EQD,EQST) 0 1 #TAG=6506# 12 1800 0 0 Set EQD=1 for Equity Desk

我写了这个:

SELECT * FROM fix_dyno_rule_defs WHERE constraint_values LIKE '%' + (SELECT MAX(SenderSubID) FROM fix_user_subids WHERE SenderSubID IN ('00390529MGERAN1','00912220PBALDIS','03994113LDAMBRO','04004308SLOMBAR','04935278CARELLI','4004308SLOMBARD')) +'%'

但是没有MAX需要它,因为有一个列表......

3 个答案:

答案 0 :(得分:3)

你没有得到理想。使用or

SELECT SenderSubID
FROM fix_user_subid
WHERE SenderSubID like '%00390529MGERAN1%' OR
      SenderSubID like '%00912220PBALDIS%' OR
      SenderSubID like '%04004308SLOMBAR%' OR
      SenderSubID like '%04935278CARELLI%' OR
      SenderSubID like '%4004308SLOMBARD%'

答案 1 :(得分:0)

如果我正确地阅读你的问题(我可能不会!),你希望从表fix_dyno_rule_defs中获得结果。

在这种情况下,你不能只做一个简单的连接吗?

即:

SELECT DISTINCT fdrd.*
FROM  fix_dyno_rule_defs fdrd 
JOIN  fix_user_subid fus 
      ON fdrd.constraint_values LIKE '%' + REPLACE(fus.SenderSubID, '*','') + '%'
WHERE fus.SenderSubID IN ('**00390529MGERAN1**','**00912220PBALDIS**',
                          '**03994113LDAMBRO**','**04004308SLOMBAR**',
                          '**04935278CARELLI**','**4004308SLOMBARD**')

答案 2 :(得分:0)

/ 这可以帮助你试试这个 声明临时表并将所有ID存储在其中 /

declare @tempSenderIDs Table(
    T_TableID varchar(10)
)

insert into @tempSender
(
    T_TableID
)
values
select 
    senderSubID
from
    SenderSubIDs

/ 最终结果将来自此处 /

select
    f.SenderSubIDs
from 
    fix_user_subid f
where 
    s.ConstraintValue like '%' + (select T_TableID from @tempSenderIDs) +'%'