基于2列减去两个表

时间:2010-08-12 13:32:59

标签: mysql

  

我需要选择行的行   'billing_temp'那个   基于的“结算”中不存在   两列。

目前我想出了这个问题:

SELECT ControlNum, CarrierName, PhoneNum, PatientName, SubscriberID, SubscriberName, 
ChartNum, DoB, SubscriberEmp, VisitID, ServiceDate, ProviderName, CPTCode, BillingDate, 
AgingDate, BalanceAmt, Age, AgeCategory FROM billing_temp LEFT JOIN billing 
USING (ControlNum, CPTCode) WHERE billing.ControlNum=billing_temp.ControlNum AND 
billing.CPTCode=billing_temp.CPTCode

但是我收到了错误:

Column 'ControlNum' in fieldlist is ambiguous.

是否有人遇到过这种情况。

如果您需要更多详细信息来恢复此信息,请通知我.. 帮我。 Thanx提前..

注意:

我很抱歉我在自己的查询中发现了什么问题。 感谢所有对我的问题感兴趣并发送答案的人。

4 个答案:

答案 0 :(得分:2)

在多个表中存在字段的字段名称(在您的查询中)的每次出现都必须是完全限定的。在您的情况下,billing.ControlNumbilling_temp.ControlNum

答案 1 :(得分:2)

如果你想选择'billing'中不存在的'billing_temp'行,我会尝试(未经测试!)的行:

SELECT
  ControlNum, CarrierName, PhoneNum, PatientName, SubscriberID, SubscriberName, 
  ChartNum, DoB, SubscriberEmp, VisitID, ServiceDate, ProviderName, CPTCode, BillingDate, 
  AgingDate, BalanceAmt, Age, AgeCategory 
FROM
  billing_temp
WHERE 
  NOT EXISTS (
    SELECT
      * 
    FROM 
      billing 
    WHERE 
      billing.ControlNum=billing_temp.ControlNum AND 
      billing.CPTCode=billing_temp.CPTCode
  )

答案 2 :(得分:2)

感谢您的所有答案。我发现我错了。 这是绝对查询:

SELECT bt.ControlNum, bt.CarrierName, bt.PhoneNum, bt.PatientName, bt.SubscriberID, 
bt.SubscriberName, bt.ChartNum, bt.DoB, bt.SubscriberEmp, bt.VisitID, bt.ServiceDate, 
bt.ProviderName, bt.CPTCode, bt.BillingDate, bt.AgingDate, bt.BalanceAmt, bt.Age, 
bt.AgeCategory FROM billing_temp bt LEFT JOIN billing ON 
bt.ControlNum=billing.ControlNum AND bt.CPTCode=billing.CPTCode

答案 3 :(得分:0)

尝试:

SELECT billing_temp.ControlNum, CarrierName, PhoneNum, PatientName, SubscriberID, SubscriberName, 
ChartNum, DoB, SubscriberEmp, VisitID, ServiceDate, ProviderName, CPTCode, BillingDate, 
AgingDate, BalanceAmt, Age, AgeCategory FROM billing_temp LEFT JOIN billing 
USING (ControlNum, CPTCode) WHERE billing.ControlNum IS NULL

规则是: 如果要选择表A中出现但未出现在表B中的所有行,则应选择“A LEFT JOIN B”中B中包含NULL的所有行。