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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two unrelated tables, one query...

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
0
0
US
I have two unrelated tables which I want to get info from for one query, each needs to be joined against another table...

I'm doing this in postgres, but I'm imagining it's a standard SQL question so I'm posting here... essentially, I want
Code:
SELECT email.email, url.url
  	FROM email, url
  	JOIN account_has_email ON contactemail.email_id = email.email_id
  	JOIN account_has_url ON account_has_url.url_id = url.url_id
  	WHERE contactemail.customer_id=5 AND account_has_url.customer_id=5

And no, there's no table where customer_id is listed to start from and join against as it's a FK to an entirely separate database. I have a feeling this is easy and I'm brainfarting, but I'd really appreciate a leg up on this one. Thanks.
 
Posted, then I figured it out... doh... for anyone who may care
Code:
SELECT email.email, url.url
  	FROM account_has_email
  	JOIN account_has_url ON account_has_email.customer_id = account_has_url.customer_id
  	JOIN email ON account_has_email.email_id = email.email_id
  	JOIN url ON url.url_id = account_has_url.url_id
  	WHERE account_has_email.customer_id = 5
Obviously some shortcoming to this method if email/url doesn't exist etc... but here's what I was looking for.
 
if there is information in one table and not in the other and you want to include that information, you have to do outer joins. See the link below for more info.



Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual

Essential reading for anyone working with databases:
The Fundamentals of Relational Database Design
Understanding SQL Joi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top