Solve “The multi-part identifier could not be boun

2019-04-13 05:22发布

问题:

select distinct 
  l.username,
  p.payid,
  p.paymentdate,
  sum(p.paymentamount) as payment,
  b.balance as balance 
from 
  tblUserLoginDetail l,
  tblInvoicePaymentDetails p 
  left outer join tblPaymentCustomerBalance b 
    on p.accountnumber=10009 
    and p.payid=b.payid 
    and p.customerid=l.loginid
  group by  p.payid,p.paymentdate,b.balance,l.username

The error is:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "l.loginid" could not be bound.

What is the solution?

回答1:

You have a cross join between tblUserLoginDetail and tblInvoicePaymentDetails in the FROM clause, so you can't use l.loginid in the FROM clause

I think what you want is this with an explicit INNER JOIN. I'e also separated filter and join conditions:

select
    l.username,
    p.payid,
    p.paymentdate,
    sum(p.paymentamount) as payment,
    b.balance as balance
from
    tblUserLoginDetail l
    inner join
    tblInvoicePaymentDetails p On p.customerid=l.loginid 
    left outer join
    tblPaymentCustomerBalance b ON p.payid=b.payid
where
    p.accountnumber=10009
group by
   p.payid,p.paymentdate,b.balance,l.username