Home > Ask the Oracle Experts > SQL Questions & Answers > Search in four separate tables
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Search in four separate tables

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 04 November 2003

I have an advertising site. The first time a person makes an ad, his email is saved, and he gets a specific password. The next time he is going to put in a new ad, he shall have the same password. The problem is that I have four tables with the same column names. How do I search in four different tables? I have tried this:

strSQL = "select fk.email, fk.kod, fs.email, 
fs.kod, L.epost, L.kod, sh.kod, sh.email
from prop_buy fk, prop_sell fs, 
LongTerm L, ShortTerm sh 
where (( fk.email = '" & session("email") & "') 
OR ( fs.email = '" & session("email") & "') 
OR (  L.email = '" & session("email") & "') 
OR ( sh.email = '" & session("email") & "' ))"

>
EXPERT RESPONSE

When you join separate tables together the way you have done, two things can happen, and both of them are usually bad.

If you leave out the join conditions, as you have done, the effect is a cross join, where you get all rows of every table matched with every row of all other tables. So if your four tables have 12, 15, 20, and 25 rows respectively, then a cross join returns 90,000 rows. Even if you restrict those 90,000 by specifying that the email must exist in one of the four fields, as you've done, there will probably still be far too many rows.

If you do include join conditions, to match rows based on some common column, like email in your case, then chances are you will get no rows back at all, because that would mean that the same email would have to exist in all four tables, whereas what you want is to find it if it exists in any of them, not all of them.

Whenever you have similar but separate tables, you should think immediately of UNION. UNION allows you to search tables separately but return everything in one query. If you wish to know which table a row came from, put an "identifying" column into the results.

select 'fk'       as identifier
     , fk.email   as email 
     , fk.kod     as kod 
  from prop_buy fk
 where fk.email = value

UNION ALL
select 'fs'   
     , fs.email
     , fs.kod
  from prop_sell fs
 where fs.email = value
UNION ALL
select 'L '   
     , L.epost
     , L.kod
  from LongTerm L
 where L.email = value
UNION ALL
select 'sh'   
     , sh.kod
     , sh.email
  from ShortTerm sh 
 where sh.email = value
order
    by 1

UNION produces a result set with column names and datatypes taken from the first subselect. Use UNION ALL to avoid an unnecessary sort to remove duplicate rows when there cannot be duplicate rows, such as in this example where the identifier column is different in each subselect. If you want the results sorted, you can have only one ORDER BY clause, and it goes at the end; use ordinal numbers to specify columns in the result set (not all database allow you to use column aliases in the ORDER BY clause).


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts