Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Join Multiple Tables

Posted on 2011-03-11
13
347 Views
Last Modified: 2012-05-11
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
Comment
Question by:aka_FATCAT
  • 3
  • 3
  • 3
  • +3
13 Comments
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35107662
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
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35107670
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
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35107675
no join condition is provided for table payment_type
therefore i ve not included that in query.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:aka_FATCAT
ID: 35107747
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35107797
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
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35107815
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
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35108690
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
 
LVL 6

Expert Comment

by:billfusion
ID: 35109542
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
 
LVL 1

Expert Comment

by:vandalesm
ID: 35109763
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
 

Author Comment

by:aka_FATCAT
ID: 35112404
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
 
LVL 6

Expert Comment

by:billfusion
ID: 35113610
So, you have a url variable named vid or state and the query does no filter those values?
0
 
LVL 6

Accepted Solution

by:
billfusion earned 250 total points
ID: 35113618
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
 

Author Closing Comment

by:aka_FATCAT
ID: 35113717
Thanks for all your help guys.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question