当前位置: 代码迷 >> Sql Server >> 寻sql语句多表查询,该如何解决
  详细解决方案

寻sql语句多表查询,该如何解决

热度:19   发布时间:2016-04-27 13:11:55.0
寻sql语句多表查询
有三个表: 
A表字段:title,mobile
B表字段:title,mobile
C表字段:mobile,phone,name
现在的情况是:要从C表中查询出mobile和phone的值并且title=title1并且mobile或者phone的值不在A表和B表的mobile值记录中.

------解决方案--------------------
select mobile into #t from A union select mobile from B

select c.name from C
where c.title = @titel1 and c.mobile not in (select mobile from #t)
and c.phone not in (select mobile from #t)

------解决方案--------------------
SQL code
SELECT c.mobile,c.phone FROM C AS c INNER JOIN A AS a                         ON c.mobile=a.mobile INNER JOIN B AS b                        ON c.mobile=b.mobile    WHERE a.title=b.title     AND NOT EXISTS (SELECT 1 FROM A WHERE c.mobile=mobile OR c.phone=phone)    AND NOT EXISTS (SELECT 1 FROM B WHERE c.mobile=mobile OR c.phone=phone)
  相关解决方案