读书人

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

发布时间: 2012-04-26 14:01:31 作者: rapoo

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

读书人网 >SQL Server

热点推荐