Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5540

c_cav on "Displaying output from one table based on the info of another"

$
0
0

First, analyze your desired output:

I want a table of data where the data consists of
a.(data)
joining.(data)
b.(data)

WHERE a.data and b.data come from either side of the join.

i.e. I don't care if person a and person b come from left side or right side of join

SELECT DISTINCT
    a.first_name AS spouse1_first_name,
    a.family_name AS spouse1_family_name,
    j.date_of_marriage,
    j.marriage_id,
    b.first_name AS spouse2_first_name,
    b.family_name AS spouse2_family_name
FROM table2 j
  INNER JOIN table1 a
    ON a.id = j.person_id OR a.id = j.spouse_id
  INNER JOIN table2 b
    ON b.id = j.person_id OR b.id = j.spouse_id

Viewing all articles
Browse latest Browse all 5540

Trending Articles