is it possile to select columns from 2 different tables?

Forum related to Firebird

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
chunkiebone
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 08.04.2013, 01:40

is it possile to select columns from 2 different tables?

Post 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.
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1956
Joined: 17.01.2011, 14:17

Post 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
Post Reply