0

所以我有一个从子查询返回的值列表,并且想从另一个表中选择与该子查询的值匹配的所有值。有没有一种最好的方法来解决这个问题?

到目前为止,我已经尝试过:

select * from table where tableid = select * from table1 where tableid like '%this%'
4

4 回答 4

1
select * from table where tableid in(select tableid 
from table1 
where tableid like '%this%')
于 2013-08-14T14:51:44.363 回答
0

我现在正在阅读一本 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%'
            )
于 2013-08-14T15:07:59.197 回答
0
select * from table where tableid IN 
(select tableid from table1 where tableid like '%this%')

子查询需要返回您要求的内容。此外,如果有超过 1 个结果,您需要IN而不是=

于 2013-08-14T14:52:47.743 回答
0

这将起作用

  select * from table where tableid in
       (select tableid from table1 where tableid like '%this%')

=仅在子查询返回 1 条记录时有效,仅
in 在子查询返回 1 条或多条记录时有效

于 2013-08-14T14:53:51.477 回答