所以我有一个从子查询返回的值列表,并且想从另一个表中选择与该子查询的值匹配的所有值。有没有一种最好的方法来解决这个问题?
到目前为止,我已经尝试过:
select * from table where tableid = select * from table1 where tableid like '%this%'
所以我有一个从子查询返回的值列表,并且想从另一个表中选择与该子查询的值匹配的所有值。有没有一种最好的方法来解决这个问题?
到目前为止,我已经尝试过:
select * from table where tableid = select * from table1 where tableid like '%this%'
select * from table where tableid in(select tableid
from table1
where tableid like '%this%')
我现在正在阅读一本 SQL Server 书籍,它强调了使用子句EXISTS
中的语句WHERE
来实现速度和效率的目的。这是一个潜在的解决方案。
SELECT
*
FROM
tableName AS t
WHERE
EXISTS(
SELECT
1
FROM
table1 AS s --as in subtable or something like that
WHERE
t.tableid = s.tableid
AND
s.tableid like '%this%'
)
select * from table where tableid IN
(select tableid from table1 where tableid like '%this%')
子查询需要返回您要求的内容。此外,如果有超过 1 个结果,您需要IN
而不是=
这将起作用
select * from table where tableid in
(select tableid from table1 where tableid like '%this%')
=
仅在子查询返回 1 条记录时有效,仅
in
在子查询返回 1 条或多条记录时有效