首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-26 
寻sql语句多表查询有三个表:A表字段:title,mobileB表字段:title,mobileC表字段:mobile,phone,name现在的情

寻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) 

热点排行