0

我的查询中有一个选择命令,它从两个表中获取数据,每行结果在结果中出现两次,我的查询是

Select  
  Driver.DriverID,
  Driver.FName,
  Driver.LName,
  Driver.SmartCardNumber,
  Driver.DriverState,
  Driver.DriverCity,
  Driver.DriverAddress,
  Driver.Mobile,
  Driver.Telephone,
  Driver.MelliCardNumber, 
  Driver.CertificatePublisher,
  Driver.addeddate,
  Driver.ContractorID,
  Driver.editeddate,
  Contractor.name  
From Driver,Contractor
4

4 回答 4

3

如果 Driver 和 Contractor 表之间存在关系,则需要 where 条件。

就像是

Select  
  Driver.DriverID,
  Driver.FName,
  Driver.LName,
  Driver.SmartCardNumber,
  Driver.DriverState,
  Driver.DriverCity,
  Driver.DriverAddress,
  Driver.Mobile,
  Driver.Telephone,
  Driver.MelliCardNumber, 
  Driver.CertificatePublisher,
  Driver.addeddate,
  Driver.ContractorID,
  Driver.editeddate,
  Contractor.name  
From Driver,Contractor
    where Driver.ContractorID = Contractor.id

假设 ContractorID 链接到 Contractor 的 id。

于 2012-09-07T15:42:13.633 回答
0

您可以尝试使用 DISTINCT。

SELECT DISTINCT  Driver.DriverID,
Driver.FName,
Driver.LName,
Driver.SmartCardNumber,
Driver.DriverState,
Driver.DriverCity,
Driver.DriverAddress,
Driver.Mobile,
Driver.Telephone,
Driver.MelliCardNumber,
Driver.CertificatePublisher,
Driver.addeddate,
Driver.ContractorID,
Driver.editeddate,
Contractor.name  

From Driver,Contractor

或者,如果您不需要 CROSS 结果,那么如果您在两个表之间有引用,则可以尝试进行 LEFT 或 INNER JOIN

于 2012-09-07T15:43:32.193 回答
0

您需要告诉 dbms 如何连接 2 个表中的记录:

例如

Select 
  Driver.DriverID,
  Driver.FName,
  ...,
  Contractor.name  
  From Driver,Contractor
  Where Driver.ContractorID = Contractor.ID;

或者

Select 
  Driver.DriverID,
  Driver.FName,
  ...,
  Contractor.name  
  From Driver inner join Contractor
  On Driver.ContractorID = Contractor.ID;
于 2012-09-07T15:45:05.430 回答
0
Select  Driver.DriverID,
Driver.FName,
Driver.LName,
Driver.SmartCardNumber,
Driver.DriverState,
Driver.DriverCity,
Driver.DriverAddress,
Driver.Mobile,
Driver.Telephone,
Driver.MelliCardNumber,
Driver.CertificatePublisher,
Driver.addeddate,
Driver.ContractorID,
Driver.editeddate,
Contractor.name  
From Driver,Contractor
where Driver.DriverId=Contractor.DriverId;

(提到一些这样的条件来获取数据)

于 2012-09-07T15:46:02.197 回答