Server Time:
Sunday May 11 2008 08:05 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Combining two queries into one..
by: Pablo Varando
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

A question I get often is if I have two databases on two separate "datasources" that have similar data, how can I create a single query from it so I can display my records easier?

Well, the easiest thing that comes to mind is the following tutorial. The first thing you will need to do is create your two queries for the two databases.

<cfquery name="query1" datasource="FirstDSN">
    SELECT         FirstName,
                       LastName,
                       Email
    FROM            Members
    ORDER BY      FirstName
</cfquery>

<cfquery name="query2" datasource="SecondDSN">
    SELECT         FirstName,
                       LastName,
                       Email
    FROM            Members
    ORDER BY      FirstName
</cfquery>

 

Now that you have your TWO queries, the next step is to create a query from scratch and input these values, that way you can output them and work with your data as if it was a single query you performed.

The first thing you must do is to create the query, you achieve this as follows:

<!--- create the new query from scratch --->
<cfset myNewQuery = QueryNew("FirstName, LastName, Email")>

Next we will create the rows for the query.

<!--- make some rows in the query --->
<cfset newRow = QueryAddRow(MyNewQuery, #query1.RecordCount# + #query2.recordCount#)>

Notice the right variables "#query1.RecordCount# + #query2.recordCount#"  this is specifying that this new query we are creatin will have a total number of rows that both of the prior queries return. This will allow this to know the new recordcount.

The next step is to actually create a counter, I will show you why this is needed in a bit, for now lets define it to have a default value of "0".

<cfset counter = 0>

The next step is to go through the first TWO queries and populate the new query with data.

<!--- set the cells in the query --->
<cfoutput query="query1">
     <cfset counter = counter + 1>
     <cfset temp = QuerySetCell(myQuery,
"FirstName", query1.FirstName, counter)>
     <cfset temp = QuerySetCell(myQuery,
"LastName", query1.LastName, counter)>
     <cfset temp = QuerySetCell(myQuery,
"Email", query1.Email, counter)>
</cfoutput>

<!--- set the cells in the query --->
<cfoutput query="query2">
     <cfset counter = counter + 1>
     <cfset temp = QuerySetCell(myQuery,
"FirstName", query2.FIrstName, counter)>
     <cfset temp = QuerySetCell(myQuery,
"LastName", query2.LastName, counter)>
     <cfset temp = QuerySetCell(myQuery,
"Email", query2.email, counter)>
</cfoutput>

<!--- Then you can use this as a regular query --->
<cfoutput query="MyNewQuery">
      #currentrow#) #name# - #ddress# - #phone#<BR>
</cfoutput>

That's pretty much it, you now have a new query called "MyNewQuery" that you can work like any other query you've ever worked with...

Questions? Comments? Email me...


Date added: Mon. March 10, 2003
Posted by: Pablo Varando | Views: 20668 | Tested Platforms: CF5,CFMX | Difficulty: Intermediate
Categories Listed: Best Practices Reusing Code Working w/Data

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Delete files and folders in a specified path!
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows! - Date added: Wed. September 7, 2005
Dynamic Last Date Modified?
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically. - Date added: Mon. April 12, 2004
Correct Content (document) serving!
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit! - Date added: Tue. February 10, 2004
Creating your very own RSS XML Feeds with ColdFusion MX!
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data! - Date added: Thu. January 15, 2004
Processing XML/RSS feeds with ColdFusion MX
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records! - Date added: Sat. December 27, 2003

Additional Tutorials:
· Changing the form submission page on the fly!

· What is the ID for the record I just inserted?

· Creating a file content crawler with ColdFusion....

· Delete Records From Your Database With ColdFusion!

· Do you want to remember your members?

· Get A Folder Size Using ColdFusion and FSO...

· Preventing People From Leeching Your Images!

· CaSe SensitiVe password logins!

· Creating an ODBC Connection within ColdFusion MX Server...

· Print your web pages on the fly!

· Using <CFPOP> and creating an email client for POP3 Email Reading!

· Using CFRegistry to Add Your IP To CF Debug IP List!

· Reading your IIS Log Files with ColdFusion!

· Automatically Adding Smiles To Your Messages!

· Using Arrays in ColdFusion To Properly Display Data....

· Implementing FORM Error Checking On Your Pages!

· Inserting FORM data into multiple database tables!

· Creating, Altering and Deleting database tables with ColdFusion.

· Sending multiple attachments with CFMAIL!

· ColdFusion and .INI Files!

· Clearing your session variables!

· Using PayPal's IPN with ColdFusion!

· Alternating Row Colors!

· Previous / Next n Records

· Using Query String Values....

· A quick intro into the world of Custom Tags!

· A brief demonstration of Fusebox 2.0

· Creating a Newsletter System....

· Count Active Users On Your Site.

· User Defined Functions....

· Creating a user athentication (Login) area.

· DSNLess Coldfusion?

· A Simple Contact Us Page….

· Having Your Database Do The Work… not ColdFusion!

· Retrieving Records From a Database..

· Inserting data into a database
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
?
cant you make a new QueryNew and do a 'select into' from the dbs into the QueryNew?
Posted by: kev
Posted on: 02/26/2005 04:28 PM
mis-spelled
when u used QuerySetCell function, you mis-spelled the query name.
Posted by: Shimju David
Posted on: 04/16/2005 02:47 AM
Easier with UNION
No need for a counter, this does it all:
<cfquery name="MyNewQuery" dbtype="query">
SELECT * FROM query1
UNION
SELECT * FROM query2
</cfquery>
Posted by: Martin
Posted on: 02/17/2008 10:49 PM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
ProWorkFlow.Com

You are 1 of 621 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb