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