Page 1 of 1

is it possile to select columns from 2 different tables?

Posted: 05.05.2013, 05:19
by chunkiebone
Hello I was just wondering if it is possible to select data from 2 tables using the zquery? I have tried this sql statement:

select * from "table1"

union

select * from "table2"


But it always gets an error. I was hoping that I can display on a TDbgrid data from 2 tables.

I am using delphi7 and firebird.

Thanks.

Posted: 07.05.2013, 15:02
by marsupilami
Hello chunkiebone,

this kind of setup works but it required that the two tables have exactly the same layout. I don't know for your database layout but you might want to use something similar to this:

Code: Select all

select cast(1 as smallint) as SRC, ID, NAME, FIRSTNAME from CONTACTS1
union all
select cast(2 as smallint) as SRC, ID, NAME, FIRSTNAME from CONTACTS2
But remember: For this to work your tables need a very similar layout - for example maybe NAME should be VARCAHR(50) on both. And it should not be VARCHAR(25) on the first and VARCHAR(50) on the second.
Best regards,

Jan