|
Thanks gary works a treat.
jason
OK - here are the tricks with UNIONS.....
* Firstly - the "types" of the data for
each column MUST be the same (best option is a string) - you can use CAST (at
least in SQL Server) to ensure the data are the right types. * Secondly - you have to have the same number of
columns in each query - SO.... make up dummy data for those tables that dont
have the same columns * Thirdly - you
can only have one ORDER BY clause *
Fourthly - it is always useful to add an additional column that has some
indicator of which sub-query the data came from - a number or a string - this
means you can then adjust your code to the "type" of data record you are
getting back.
For example:
Table 1 - has 3 integer fields
Table 2 - has 2 integer fields and 3
strings Table 3 - has 2 real values
and a bit field
The UNION goes a
little like this (NOTE - this is NOT really valid SQL but it's close):
SELECT 'Table1' as recordType, CAST(int1 as real) as
real1,
CAST(int2 as real) as real2,
CAST(int2 as integer) as int1, '' as string1,
'' as
string2,
'' as string3,
0 as bit1 FROM table1 UNION SELECT
'Table2' as recordType,
CAST(int1 as real) as real1, CAST(int2 as real) as
real2, 0
as int1,
string1,
string2,
string3, 0
as bit1 FROM table2 UNION SELECT
'Table3' as recordType,
CAST(real1 as real) as real1, CAST(real2 as real) as
real2, 0
as int1,
'' as string1,
'' as string2,
'' as string3,
bit1 FROM
table3
ORDER BY recordType
NOW..... that is only just ONE way to do it. You
could put NULL's into all the fields that are "missing" from the respective
tables. You could duplicate ALL missing fields across each of the
sub-queries. The possibilities are probably
NUMBER_OF_FIELD^NUMBER_OF_TABLES.
But I have never found something that couldn't be coded up as as UNION
if you tried hard enough.
HOWEVER.....
If you want,
you could do the three separate queries and then manually copy the results
into a QUERY that you create yourself in CF and then return that.
Gary Menzel IT Operations Brisbane
-+- ABN AMRO Morgans Limited Level 29, 123 Eagle Street BRISBANE QLD
4000 PH: 07 333 44 828 FX: 07 3834 0828
[EMAIL PROTECTED] wrote on 03/21/2003
12:26:25 PM:
> Hi Guys > > Anyone know of any easy way
to query 3 different tables, but outputting the > results of a search as
one single record set ? > I have tried unions, running 3 seperate select
statements inside the one > <cfquery>, the tables are quite
different in structure so the union doesnt > want to play.. There
is no relational aspects between the tables so a join > wont work
either. > > Is there a way to run 3 seperate queries and then
combine the results into a > single collection for output? I want to do
the next/previous across the > search results page also hence the
complexity.. > > Any ideas or thoughts appreciated.. >
> Jason > > > > --- > You are
currently subscribed to cfaussie as: [EMAIL PROTECTED] > To
unsubscribe send a blank email to
[EMAIL PROTECTED] > > MX Downunder AsiaPac
DevCon - http://mxdu.com/ **************************************************************************** If this communication is not intended for you
and you are not an authorised recipient of this email you are prohibited by law from dealing with
or relying on the email or any
file attachments. This prohibition includes reading, printing, copying, re-transmitting,
disseminating, storing or in any other way dealing or acting in reliance on the information. If
you have received this email in
error, we request you contact ABN AMRO Morgans Limited immediately by returning the email to
[EMAIL PROTECTED] and
destroy the original. We will refund any reasonable costs
associated with notifying ABN
AMRO Morgans. This email is confidential and may contain privileged client information. ABN AMRO Morgans
has taken reasonable steps to
ensure the accuracy and integrity of all its communications,
including electronic
communications, but accepts no liability for materials transmitted. Materials may also be transmitted
without the knowledge of ABN AMRO Morgans. ABN AMRO Morgans Limited its directors and employees do
not accept liability for the
results of any actions taken or not on the basis of the information in this report. ABN AMRO Morgans
Limited and its associates hold
or may hold securities in the companies/trusts mentioned herein. Any recommendation is made on the basis
of our research of the investment and may not suit the specific requirements of
clients. Assessments of
suitability to an individual's portfolio can only be made after an examination of the particular client's
investments, financial circumstances and requirements. ****************************************************************************
---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
MX Downunder AsiaPac DevCon - http://mxdu.com/
|