SQL Server从子表中的列所在的列中选择列

时间:2013-01-17 09:49:36

标签: sql-server-2008 subquery

我的查询结果错误,因为我没有得到任何结果,但绝对存在。

QUERY

 select nr 
 from table1 
 inner join table2 on table2.nr = table1.nr
 where table1.nr in (select nr 
                     from table2 
                     where columnn like '%value%') 
   and nr in (select nr from table2 where columnn like '%other value%')

当我只使用第一个子查询时,我得到了结果,但是其中的第二个子查询我没有

1 个答案:

答案 0 :(得分:2)

使用OR而不是AND

select nr from table1 
 inner join table2 on table2.nr = table1.nr
 where table1.nr in (select nr from table2 where columnn like '%value%') or nr in 
(select nr from table2 where columnn like '%other value%')

如果与你使用的查询完全相同,则加入是无用的。

优雅的方式是

select nr from table1 
 inner join table2 on table2.nr = table1.nr
 where CONTAINS(table2.column, '"*value*" OR "*other value*"')