Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query help across 3 tables.

Status
Not open for further replies.

cl8855

Programmer
Nov 28, 2000
47
US
Alerts Table:
alertId, col1, col2, col3

Msgs Table:
alertId, MsgId

Comments Table:
Msgid, col1, col2, col3, col4


Is there a way to build just one query that grabs
all columns/records from the alerts table for "col1 > 1", AND
all columns from the comments table for that record's corresponding AlertId/MsgId from the Msgs table?

Right now I am looping through the query of the alerts table and querying the comments table, but that is too slow.

 
OK, cool I think that will work.

How about I make it harder then!

I want to get a.* as long as a.col1 > 1, but no matter whether (a.alertid = or <> m.alertid, m.msgid = or <> c.msgId)

Of course if the others are correct then I want to get c.*
 
If you don't want a cartesian product of a.* c.*, you can use UNION.
Code:
SELECT a.*, c.*
FROM alert a,
  message m,
  comment c
WHERE a.alertid = m.alertid
  AND m.msgid = c.msgId
  AND a.col1 != 1
UNION 
SELECT a.*, "foo", "foo", "foo"
FROM alert a
WHERE a.col1 > 1;
Is this a game?

seeking a job as java-programmer in Berlin:
 
Hmm I don't think that works, I thought Union needed same data types on the columns.

What is the "foo" stuff you are adding? Trying to get the columns to match in the union?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top