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
Best Practices
Reusing Code
Working w/Data
?
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
|
|