On Fri, 2007-05-04 at 18:22 -0400, Vitali Lovich wrote:
> Hi,
> 
> I was wondering what would be the optimal way to select 1 matching row 
> from multiple tables.  Here is the scenario.
> 
> Multiple tables contain a primary key KEY.  If Table1 contains a 
> matching KEY, then I want that row from Table1.  Only if it isn't in 
> Table1, then look in Table2 if it is there.  If not in Table2 go on to 
> Table3, etc etc.
> 
> Is there a way to do this using SQL, or should I just break this up into 
> multiple queries and have the logic in C - this is for an embedded 
> system, so I want to use the least amount of memory & CPU (memory is 
> more important though).

How about this:

  SELECT * FROM tbl1 WHERE key = $key 
  UNION ALL 
  SELECT * FROM tbl2 WHERE key = $key
  LIMIT 1;

Although technically, using LIMIT without ORDER BY is a bad thing.





> 
> Thanks
> 
> -----------------------------------------------------------------------------
> To unsubscribe, send email to [EMAIL PROTECTED]
> -----------------------------------------------------------------------------
> 


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to