×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Two unrelated tables, one query...

Two unrelated tables, one query...

Two unrelated tables, one query...

(OP)
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.

RE: Two unrelated tables, one query...

(OP)
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.

RE: Two unrelated tables, one query...

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 Joins

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close