如何防止转发器表中的重复行?

时间:2013-01-08 16:05:25

标签: sql

我一直收到语法错误

  

*关键字'SELECT'附近的语法不正确。

     

')'。*

附近的语法不正确

当使用where where子句时,我使用正确的格式存在。

SELECT 
   tblLoadMaster.Salesperson, tblLoadMaster.LoadID, LoadRate, 
   PayBetween1, Paybetween2, 
   tblLoadMaster.BillingID, OriginationCity, OriginationState, 
   DestinationCity, DestinationState 
FROM tblLoadMaster
WHERE EXISTS (SELECT tblCarrier.CompanyName, tblCarrier.CarrierID 
              FROM tblCarrier 
              WHERE tblLoadMaster.CarrierID = tblCarrier.CarrierID)
  AND EXISTS (SELECT tblCustomer.CompanyName, tblCustomer.Customerflag, tblCustomer.CustomerID, tblCustomer.AddressLine1, tblCustomer.City, tblCustomer.State, tblCustomer.Zipcode, tblCustomer.CompanyPhoneNumber, tblCustomer.CompanyFaxNumber, tblCustomer.SCAC 
              FROM tblCustomer 
              WHERE tblCustomer.CustomerID = tblLoadMaster.CustomerID )
  AND EXISTS(SELECT StopLocationID FROM tblLoadStop 
             WHERE tblLoadMaster.LoadID = tblLoadStop.LoadID) 
  AND EXIST(SELECT StopLocationCompanyName 
            FROM tblStoplocation 
            WHERE tblStopLocation.StoplocationID = tblLoadStop.StoplocationID 
              AND tblLoadMaster.Phase LIKE '%4%')  
ORDER BY 
    tblCarrier.CarrierID DESC;

更新

我使用了INNER JOIN语句,但仍然获得了重复的行。 tblLoadStop表是我的查找表,用于获取stoplocationstoplocation ID的{​​{1}}信息。 load ID表包含tblLoadstop两次

LOADID

转发器然后在表格中显示两行,只有LoadStopID(pk) LoadID StopLocationID 476 13521 67 477 13521 106 相同。

这是我的LoadID声明;

INNER JOIN

2 个答案:

答案 0 :(得分:1)

也许这是最后一个“EXISTS”的拼写错误被写成“EXIST”而不是

SELECT tblLoadMaster.Salesperson,  tblLoadMaster.LoadID,LoadRate,PayBetween1, Paybetween2 , tblLoadMaster.BillingID, OriginationCity,OriginationState, DestinationCity, DestinationState 
FROM tblLoadMaster 
 WHERE EXISTS 
    (SELECT tblCarrier.CompanyName,tblCarrier.CarrierID FROM tblCarrier WHERE tblLoadMaster.CarrierID =tblCarrier.CarrierID )
 AND EXISTS 
  (SELECT tblCustomer.CompanyName, tblCustomer.Customerflag, tblCustomer.CustomerID, tblCustomer.AddressLine1, tblCustomer.City, tblCustomer.State, tblCustomeripcode, tblCustomer.CompanyPhoneNumber, tblCustomer.CompanyFaxNumber, tblCustomer.SCAC FROM tblCustomer WHERE tblCustomer.CustomerID=tblLoadMaster.CustomerID )
 AND EXISTS
    (SELECT StopLocationID FROM tblLoadStop WHERE tblLoadMaster.LoadID=tblLoadStop.LoadID) 
 AND EXISTS
  ( SELECT StopLocationCompanyName FROM tblStoplocation WHERE tblStopLocation.StoplocationID=tblLoadStop.StoplocationID AND  tblLoadMaster.Phase LIKE '%4%') 
ORDER BY tblCarrier.CarrierID DESC;

BTW,我不确定您使用的是哪个数据库,但我更喜欢在exists子句中使用“从表中选择1”。它似乎更具可读性,你马上知道你只想知道某些东西是否会返回,所选的列根本不重要

SELECT tblLoadMaster.Salesperson,  tblLoadMaster.LoadID,LoadRate,PayBetween1, Paybetween2 , tblLoadMaster.BillingID, OriginationCity,OriginationState, DestinationCity, DestinationState 
FROM tblLoadMaster 
 WHERE EXISTS 
    (SELECT 1 FROM tblCarrier WHERE tblLoadMaster.CarrierID =tblCarrier.CarrierID )
 AND EXISTS 
  (SELECT 1 FROM tblCustomer WHERE tblCustomer.CustomerID=tblLoadMaster.CustomerID )
 AND EXISTS
    (SELECT 1 FROM tblLoadStop WHERE tblLoadMaster.LoadID=tblLoadStop.LoadID) 
 AND EXISTS
  ( SELECT 1 FROM tblStoplocation WHERE tblStopLocation.StoplocationID=tblLoadStop.StoplocationID AND  tblLoadMaster.Phase LIKE '%4%') 
ORDER BY tblCarrier.CarrierID DESC;

这不是更漂亮吗?

答案 1 :(得分:0)

你说EXIST,而不是下面的EXISTS: (更改为粗体)

"SELECT 
     tblLoadMaster.Salesperson,  tblLoadMaster.LoadID,
     LoadRate, PayBetween1, Paybetween2 , tblLoadMaster.BillingID, 
     OriginationCity,OriginationState, DestinationCity, DestinationState 
 FROM 
   tblLoadMaster "+
"WHERE 
     EXISTS (SELECT 
                   tblCarrier.CompanyName,tblCarrier.CarrierID 
             FROM 
                    tblCarrier 
             WHERE 
                    tblLoadMaster.CarrierID =tblCarrier.CarrierID )" +
 "AND 
     EXISTS(SELECT 
                  tblCustomer.CompanyName, tblCustomer.Customerflag, 
                  tblCustomer.CustomerID,
                  tblCustomer.AddressLine1, tblCustomer.City, tblCustomer.State, 
                  tblCustomer.Zipcode, tblCustomer.CompanyPhoneNumber,
                  tblCustomer.CompanyFaxNumber, tblCustomer.SCAC 
            FROM 
                  tblCustomer 
            WHERE 
                  tblCustomer.CustomerID=tblLoadMaster.CustomerID )" +
 "AND EXISTS(SELECT 
                  StopLocationID 
             FROM 
                  tblLoadStop 
             WHERE 
                  tblLoadMaster.LoadID=tblLoadStop.LoadID) 
   AND **EXISTS**(SELECT 
                        StopLocationCompanyName 
                   FROM 
                        tblStoplocation 
                   WHERE 
                         tblStopLocation.StoplocationID=tblLoadStop.StoplocationID 
                         AND  
                       tblLoadMaster.Phase LIKE '%4%')  
                   ORDER BY 
                       tblCarrier.CarrierID DESC;");