• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Join Multiple Tables

I'm getting this error.

The multi-part identifier "invoice_payments.inv_id" could not be bound.

Need help joining these tables together.

Please assist.
<cfquery name="invoices" datasource="#request.dataSource#">
SELECT *
FROM invoice, vendor, invoice_payments, payment_type, payments
LEFT OUTER JOIN invoice ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
WHERE vendor.vid = invoice.vid 
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
ORDER BY vendor.v_name, invoice_date  
</cfquery>

Open in new window

0
aka_FATCAT
Asked:
aka_FATCAT
  • 3
  • 3
  • 3
  • +3
2 Solutions
 
mayank_joshiCommented:
try:-

SELECT *
FROM  invoice_payments
LEFT OUTER JOIN invoice ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
inner join vendor on vendor.vid = invoice.vid 

Open in new window


0
 
pdd1lanCommented:
is the field name "inv_id" in other tables beside "Invoice_payments" table?

my suggest is not to use SELECT *, if you have a small database, it might not make different, but if you have a large database, display all the fields in all your join tables, perform might be slow.  

SELECT invoice_payments.inv_id,payments.p_id ,....
FROM invoice, vendor, invoice_payments, payment_type, payments
....
0
 
mayank_joshiCommented:
no join condition is provided for table payment_type
therefore i ve not included that in query.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
aka_FATCATAuthor Commented:
I'm still getting the same error message.
<cfquery name="invoices" datasource="#request.dataSource#">
SELECT invoice_number, v_name, payment_date, payment_amount
FROM invoice, vendor, invoice_payments, payment_type, payments
LEFT OUTER JOIN invoice ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
LEFT OUTER JOIN payment_type ON payment_type.pt_id = payments.payment_type
INNER JOIN vendor ON vendor.vid = invoice.vid 
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
ORDER BY vendor.v_name, invoice_date  
</cfquery>

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
your FROM part is wrong...
<cfquery name="invoices" datasource="#request.dataSource#">
SELECT invoice_number, v_name, payment_date, payment_amount
FROM invoice 
INNER JOIN vendor ON vendor.vid = invoice.vid 
LEFT OUTER JOIN invoice_payments ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
LEFT OUTER JOIN payment_type ON payment_type.pt_id = payments.payment_type
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
ORDER BY vendor.v_name, invoice_date  
</cfquery>

Open in new window

0
 
mayank_joshiCommented:
why are you using table name for more than once?

try:-

SELECT invoice_number, v_name, payment_date, payment_amount
FROM invoice_payments
LEFT OUTER JOIN invoice ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
LEFT OUTER JOIN payment_type ON payment_type.pt_id = payments.payment_type
INNER JOIN vendor ON vendor.vid = invoice.vid 

Open in new window

0
 
pdd1lanCommented:
can you try it without "ORDER BY"?

<cfquery name="invoices" datasource="#request.dataSource#">
SELECT *
FROM invoice, vendor, invoice_payments, payment_type, payments
LEFT OUTER JOIN invoice ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
WHERE vendor.vid = invoice.vid
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
</cfquery>
0
 
billfusionCommented:
This may sound like a silly question but I have to ask, are you sure the invoice id field in invoice_payment is inv_id not something else.  I just recreated the tables based on your query and it worked OK for me.  Just to be sure, use the field and table names that are not working exactly including case sensitivity.

Here is the query I used:

SELECT     invoice.invoice_number, vendor.v_name, payments.payment_date, payments.payment_amount
FROM         invoice CROSS JOIN
                      vendor CROSS JOIN
                      invoice_payments CROSS JOIN
                      payment_type CROSS JOIN
                      payments LEFT OUTER JOIN
                      invoice invoice_1 ON invoice.invoice_id = invoice_payments.inv_id LEFT OUTER JOIN
                      payments payments_1 ON payments.p_id = invoice_payments.p_id LEFT OUTER JOIN
                      payment_type payment_type_1 ON payment_type.pt_id = payments.payment_type INNER JOIN
                      vendor vendor_1 ON vendor.vid = invoice.vid AND invoice.invoice_status = 1 AND invoice.vid = 1
ORDER BY vendor.v_name, invoice.invoice_date
0
 
vandalesmCommented:
Try putting an table alias in every table of your query.

The error might be that you have column name called "invoice_paments" which conflicts the name of your table.
0
 
aka_FATCATAuthor Commented:
angelIII, your code is working great.

The only part that isn't working is the <cfif> part. It doesn't work and it doesn't report any errors.
<cfquery name="invoices" datasource="#request.dataSource#">
SELECT invoice_number, v_name, payment_date, payment_amount, invoice_amount, invoice.vid, invoice_date, check_number, payment_type_name, invoice_status, invoice_id
FROM invoice 
INNER JOIN vendor ON vendor.vid = invoice.vid 
LEFT OUTER JOIN invoice_payments ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
LEFT OUTER JOIN payment_type ON payment_type.pt_id = payments.pt_id
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
ORDER BY vendor.v_name, invoice_date  
</cfquery>

Open in new window

0
 
billfusionCommented:
So, you have a url variable named vid or state and the query does no filter those values?
0
 
billfusionCommented:
continuing my last comment.  Those statements may fit better in the where clause:


<cfquery name="invoices" datasource="#request.dataSource#">
SELECT invoice_number, v_name, payment_date, payment_amount, invoice_amount, invoice.vid, invoice_date, check_number, payment_type_name, invoice_status, invoice_id
FROM invoice
INNER JOIN vendor ON vendor.vid = invoice.vid
LEFT OUTER JOIN invoice_payments ON invoice.invoice_id = invoice_payments.inv_id
LEFT OUTER JOIN payments ON payments.p_id = invoice_payments.p_id
LEFT OUTER JOIN payment_type ON payment_type.pt_id = payments.pt_id
Where
<cfif isDefined ('URL.state')>
AND invoice_status = '#URL.state#'
</cfif>
<cfif isDefined ('URL.vid')>
AND invoice.vid = '#URL.vid#'
</cfif>
ORDER BY vendor.v_name, invoice_date  
</cfquery>
0
 
aka_FATCATAuthor Commented:
Thanks for all your help guys.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now