Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8306849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:24:42+00:00 2026-06-08T18:24:42+00:00

I got 4 tables, Email_Company_Contact_Ref table is the table which linked with Email, Company,

  • 0

I got 4 tables, Email_Company_Contact_Ref table is the table which linked with Email, Company, and Contact.

**Email_Company_Contact_Ref**

id = primary key
email_id = reference to Email.`id`
ref_id = it can be Company.id / Contact.id
table = reference from which table name

table structure

I try to use left join to get my output, but I got duplicated result. If I try inner join, I will not get any result at all, it is because Company and Contact this two tables does not have any thing common.

This is the output I would like to complete.

output result

I able to use UNION to get the output, but it not really effective. I am thinking it should be a way to get the output result.. Please help.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:24:43+00:00Added an answer on June 8, 2026 at 6:24 pm

    Here is my mysql answer, hope this can help

    SELECT e.email, r.table, c1.name AS company_name, c2.name AS contact_name
    FROM email_company_contact_ref r
    JOIN email e ON e.id = r.email_id
    LEFT JOIN company c1 ON (c1.id = r.ref_id AND r.table = 'company')
    LEFT JOIN contact c2 ON (c2.id = r.ref_id AND r.table = 'contact')
    GROUP BY r.table, e.email
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a table [newsletter] in the db, which saves the email address,
I have got this table which has email address of 3 employees in SQL
I've got tables like: table A id name email 1 test1 ex@ex.com 2 test2
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I got two tables, one of which is a helper of the other. The
I've got two tables: TABLE A (columns: FirstValue, SecondValue, SeatNumber) currently empty TABLE B(1
I've got 2 tables. The first table is full of entries. The second table
I have got a table structure which is as follows: `CREATE TABLE "pages" (
I'm wondering how I can normalize my tables in rails, say I've got a
I got 3 tables in sql, which looks like this: (need sql syntax) 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.