0

我有表1:

id|name|city
-------------
1 | bat|nyc
2 | cat|del
2 | rat|bom
3 | mat|xyz

表2

id|name|city
--------------
1 |bat|dsada
2 |cat|sadasd
3 |rat|sdasdas
2 |rat|asdas

我需要一个查询来显示上面示例中两个表中 id 和 name 都相同的行查询必须返回我需要一个查询来显示行

1|bat|nyc
2|cat|del 

从第一张桌子

4

4 回答 4

0

最简单的(可能也是最有效的)是使用EXISTS

SELECT id,name,city FROM Table1 t1
WHERE EXISTS
(
   SELECT 1 FROM Table2 t2
   WHERE t2.name = t1.name
   AND t2.id  = t1.id
)
于 2012-10-26T09:59:21.190 回答
0

尝试加入两个表,如下所示

select tabl1.* from table1
inner join table2
on table1.id=table2.id and table1.name=table2.name
于 2012-10-26T09:59:25.897 回答
0
select
   T1.id, T1.name
from Table1 as T1
where
    exists (select * from Table2 as T2 where T2.id = T1.id and T2.name = T1.name)
于 2012-10-26T09:59:31.813 回答
0

试试这个: JOIN 将确保满足两个条件,即来自两个表的两条记录具有相同的idname

Select t1.name, t1.city from Table1 t1
 Join Table2 t2
  ON t1.id = t2.id and 
        t1.name = t2.name
于 2012-10-26T09:59:55.443 回答