SQL子查询问题不会产生任何错误和结果

时间:2015-09-16 22:55:01

标签: sql-server subquery

我正在编写一个简单的select子查询语句,它没有给出任何结果。它都没有抛出任何错误。 我的Sql查询是这样的 -

select * from Table1
where id in (select ID from Table2 where user = 'xyz')

我也尝试过存在,但没有显示任何结果。 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

可能是以下子查询返回一个空列表,因为过滤条件where user = 'xyz'不匹配任何记录

select ID from Table2 where user = 'xyz'

以下外部查询并不匹配条件where id in ()

select * from Table1 where id in ()

因此返回一个空结果集。

您可以将发布的查询转换为INNER JOIN查询,例如

select t1.* 
from Table1 t1 join Table2 t2 
on t1.id = t2.id
where t2.user = 'xyz';

作为测试,请尝试使用LIKE运算符代替相等比较

select * from Table1
where id in (select ID from Table2 where user like '%xyz%')