Skip to content
Advertisement

Join Only Unique Values From 2 Tables

I’m trying to join two MySQL tables, and return only the unique values between them. eg.

I want to get as my result a table that only shows the intersections of rows without any duplicates where I’m joining on table1.column1 = table2.column1:

Simple joins and unions and I’m ok, but this one is causing me a headache.

Edit for clarification: I don’t want any results in my joined table where Table 2 has > 1 entry in column 1. I only want to get back the values from column2 for c1value2 and c1value3.

My first thought was to get the distinct count of column2 GROUP BY column1 WHERE distinct count = 1 but that’s error city.

Advertisement

Answer

Are you trying something like this:

Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/70

Edit: If you want the unique values of column2 of both tables based on column1 of table1, you can use a subquery for selecting the distinct values for column1 and use it in the where condition.

Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/72

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement