views:

31

answers:

4

Hi All,

I need to SELECT the rows of 'billing_temp' that doesn't exist in 'billing' based on two columns.

Currently I come-up with this query:

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

But I'm getting the error:

Column 'ControlNum' in fieldlist is ambiguous.

Does anyone faced this occurrence.

If you need further details to recover this, inform me.. Help me. Thanx in advance..

NOTE :

I'm so sorry that I've found what is the problem in my own query.. Thanks for all who paid interest in my question and sent the answer..

+2  A: 

Every occurence of a field name (in your query) where the field exists in more than one table must be fully qualified. In your case, either billing.ControlNum or billing_temp.ControlNum

Tobiasopdenbrouw
Sorry bro, I can't understand. Could u pls describe what you've replied...
venJava
@ven You have two columns called "ControlNum" in your query, `billing.ControlNum` and `billing_temp.ControlNum`. When you start your query with `SELECT ControlNum...`, MySQL doesn't know which one you want to select. Pick one, and use it, e.g. `SELECT billing.ControlNum...`
Matt Gibson
I think they're all going to be ambiguous. @ven: You're going to have to fully qualify all of your column names, or if you just want all of them from one of the tables, do `SELECT billing.* FROM ...` or `SELECT billing_temp.* FROM ...`.
Cory Larson
@Matt I've tried with 'billing_temp.ControlNum' in 'SELECT', but the same error occurred.. Can you identify why?
venJava
Your USING also has the field.
Tobiasopdenbrouw
@Tobia - What you come to say? Whether I should use it (USING clause) or not?? Please make clear statement..
venJava
As @Tobias says, `ControlNum` also appears in your USING clause. Again, the SQL parser doesn't know which of the two fields you mean. So, use `USING (billing.controlNum, CPTCode)`, for example.Really, the take-away here is that you keep on saying `controlNum` without letting SQL know *which* `controlNum` you mean. It could be either, so SQL complains unless you tell it which one you want to use. That's pretty much what "ambiguous" means -- it could be one thing or the other.
Matt Gibson
Yes, what Matt said.
Tobiasopdenbrouw
Thanks for your helpful idea.. I've found where did I mistake..
venJava
A: 

Try:

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

The rule is: When you want to select all rows that appear in table A but do not appear at table B, you should select all the rows from "A LEFT JOIN B" that have NULLs in B.

AndreyKo
I need to SELECT the rows from 'billing_temp' which are not in 'billing'. I've tried still the same error persist...
venJava
+2  A: 

If you want to SELECT the rows of 'billing_temp' that doesn't exist in 'billing', I'd try something along the lines of (untested!):

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
  )
Matt Gibson
Yes, I'd be doing that as well. +1
Tobiasopdenbrouw
Thank you, bro. But, I want to isolate what's the problem in the query that I framed??
venJava
A: 

Hi Intellects,

Thanks for all of your answers. I've found where I did mistake. Here's the absolute query:

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
venJava