Solved

Join Multiple Tables

Posted on 2011-03-11
13
344 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
Comment Utility
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
Comment Utility
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
Comment Utility
no join condition is provided for table payment_type
therefore i ve not included that in query.
0
 

Author Comment

by:aka_FATCAT
Comment Utility
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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:pdd1lan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now