如何解决选择Sql查询问题

时间:2013-11-18 06:30:43

标签: sql sql-server-2008

大家好,在select查询中存在问题。该场景是我有两个表格Customerprofilepropertyalloted。我想只选择那些customer name表中不存在的propertyalloted

这是我的查询请告诉我现有查询的更改

select distinct 
    (a.ApplicationNo),
    a.Serial,
    b.Name,
    a.CustomerName,
    Convert(varchar(10),a.DOB,103) as DOB ,
    a.Nationality,
    a.Age,
    a.Profession,
    a.Password,                   
    a.ResidentialStatus,
    a.PanNo,
    a.MailingAdd1,
    a.MailingAdd2,
    a.TelNo,
    a.MobileNo,
    a.FaxNo,
    a.OfficeAdd1,
    a.CusId,                      
    a.OfficeAdd2,
    a.OfficeTel,
    a.OfficeFax,
    a.EMail,
    a.SO,
    a.PassportNo,
    a.CustPicture                        
 from CustomerProfile a,FranchiseProfile b                        
 where  a.FranchiseId=b.FId 

2 个答案:

答案 0 :(得分:0)

使用NOT EXISTS

SELECT t.*
  FROM table t
 WHERE NOT EXISTS(SELECT NULL
                    FROM table1 lt
                   WHERE lt.customer_name = t.customer_name)

使用NOT IN

SELECT t.*
  FROM table t
 WHERE t.customer_name NOT IN (SELECT lt.customer_name
                             FROM table1 lt)

使用joins

SELECT t1.id, t1.customer_name
FROM table1 AS t1
LEFT  JOIN table2 AS t2 ON t1.id = t2.id
WHERE t2.id IS NULL

答案 1 :(得分:0)

您在查询中提供了以下表格:CustomerProfile , FranchiseProfile但问题描述为:registered and propertyalloted.如果您正在查找以下内容,则应用交叉联接:从{选择所有客户名称{1}}表不在FranchiseProfile表中,然后添加如下的子查询可能会有所帮助:

CustomerProfile