在like语句中的内部查询

时间:2014-03-07 06:08:30

标签: sql sql-server database tsql

我有一个类似的查询:

SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data like '%2012177%'

这里而不是硬编码dataId值'2012177',我需要从查询中获取此值,如:

SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01'.

我该怎么做?

3 个答案:

答案 0 :(得分:0)

你可能会尝试:

SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data in 
(SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01')

答案 1 :(得分:0)

SELECT m.data 
FROM MSG_1 m
JOIN (SELECT distinct dataId as d 
      FROM Item 
      where Src='MKT' and ValueDt>'2014-02-01'
     ) t
ON m.data like '%'+t.d+'%'
WHERE m.created>'2014-02-24'

编辑以适应sql server。

答案 2 :(得分:0)

希望这会有所帮助:
选择
数据
FROM
MSG_1 M1
JOIN(选择不同的dataId FROM Item,其中Src ='MKT'和ValueDt>'2014-02-01')M2
ON M1.Data = concat('%',M2.dataId,'%')
WHERE      
M1.created>'2014-02-24'