I have multiple tables that I need to join and display, however I am having trouble getting exactly what I want. Both tables are identical in headings, though represent data for different events, and look like this:
I am looking to combine the two tables on the FirstName
, LastName
and City
columns, and rename the remaining column tables to prepend the event each represents. When I try to CROSS JOIN
the two tables, I get the cartesian product of the two, so I added a WHERE
clause to only show rows where the three columns matched. While this helped significantly, I am still left with duplicate rows and have been unable to get rid of them:
This table structure/context is what I need, though as mentioned before, the issue is I don't want to display the duplicate rows.
Here is my query:
SELECT event1.FirstName, event1.LastName, event1.City, event1.AdultsAttending as Event1_AdultsAttending, event1.AdultsInvited as Event1_AdultsInvited, event1.Attending as Event1_Attending, event1.ChildrenAttending as Event1_ChildrenAttending, event1.ChildrenInvited as Event1_ChildrenInvited, event2.AdultsAttending as Event2_AdultsAttending, event2.AdultsInvited as Event2_AdultsInvited, event2.Attending as Event2_Attending, event2.ChildrenAttending as Event2_ChildrenAttending, event2.ChildrenInvited as Event2_ChildrenInvited
FROM `event1`
CROSS JOIN `event2`
WHERE event1.FirstName=event2.FirstName AND event1.LastName=event2.LastNameAND event1.City=event2.City
How can I modify this query to get the results I am looking for?
EDIT
Here is a SQLFiddle with my schemas: http://sqlfiddle.com/#!9/770473/2 This is my desired output: