sql查询两个表的记录!!高手速度进来!!

来源:百度知道 编辑:UC知道 时间:2024/06/16 07:38:11
有两个表A和B,A表和B都有一个共同的字段phone
我想查A表里所有值,条件是A表的phone必须在B表phone里有!查询语句怎么写!
我试了很多方法不行!

两个表没有任何关系!

select * from A where phone in (select phone from B)

select * from A where phone in (select distinct phone from B)

1、select 需要的字段 from a join b on a.phone=b.phone
2、select 需要的字段 from a,b where a.phone=b.phone
3、select 需要的字段 from a where charindex(a.phone,b.phone)>0

建议不要用*号查询,因为那样查出来的东西太多,有些不是你想要的。数据查得越多越慢,而且传输速度也会有影响。把你需要查询的字段写出,即使查询所有字段,速度也会比*快,当然你写代码的时候会麻烦一点,一不注意也比较容易出来一点拼写错误