Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I have a little problem here. I'm trying to search the data of an object in the database. The problem is, the data is placed in 3 table with identical structure.

I tried this query :
SQL
SELECT   *
  FROM   SPOHON S, APOHON A, LPOHON L
 WHERE   GEOID = '227447'

And it failed, it gives an error saying an unambiguous column exist (or something like that)

And then I tried this one :
SQL
SELECT   *
  FROM   SPOHON S, APOHON A, LPOHON L
 WHERE   S.GEOID = '227447' OR A.GEOID = '227447' OR L.GEOID = '227447';

I failed again, it doesn't return any results (The data is located in table LPOHON)

So, is there any SQL solution for this? or do I have to do my work around like I always do?

Thanks in advance.
Posted

I guess you can use UNION in this, like

Select * from SPOHON where GEOID=227447
UNION
Select * from APOHON where GEOID=227447
UNION
Select * from LPOHON where GEOID=227447

Hope that answers your questions
 
Share this answer
 
Comments
Wendelius 9-Sep-11 1:25am    
Good anser, + 5. Just added few more things, please see my answer.
Firo Atrum Ventus 9-Sep-11 1:37am    
Thanks.
To add to Vikash answer:

Don't use * in the SELECT clause. For example if a column is added to one of the tables, your query would fail since in UNION all the select statements must return exactly same number of columns and same data types

Another thing. Don't use literals in the query if you're putting it in a program, use bind variables instead. If you're using ODP.NET, see: OracleParameter[^]. Your query would then look something like:
SQL
Select col1, col2 from SPOHON where GEOID=:geoid
UNION
Select col1, col2 from APOHON where GEOID=:geoid
UNION
Select col1, col2 from LPOHON where GEOID=:geoid
 
Share this answer
 
Comments
Mehdi Gholam 9-Sep-11 1:20am    
My 5!
Wendelius 9-Sep-11 1:25am    
Thanks :)
Firo Atrum Ventus 9-Sep-11 1:39am    
Thanks.
That's a good link you got there.
Wendelius 9-Sep-11 2:42am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900