简体   繁体   English

仅连接 2 个表中的唯一值

[英]Join Only Unique Values From 2 Tables

I'm trying to join two MySQL tables, and return only the unique values between them.我正在尝试连接两个 MySQL 表,并仅返回它们之间的唯一值。 eg.例如。

Table1
column1|column2
-------------
c1value1|c2value1-1
c1value2|c2value1-2
c1value3|c2value1-3
Table2
column1|column2
-------------
c1value1|c2value1-1
c1value1|c2value2-1
c1value1|c2value3-1
c1value2|c2value1-2
c1value3|c2value1-3

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:我想得到一个表,该表显示行的交集,而没有任何重复项,其中我在 table1.column1 = table2.column1 上加入:

Joined Table
table1.column2|table2.column2
-----------------------------
table1.c2value1-2|c2value1-2
table1.c2value1-3|c2value1-3

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.编辑澄清:我不希望在表 2 的第 1 列中有 > 1 个条目的连接表中有任何结果。我只想从 column2 中取回 c1value2 和 c1value3 的值。

My first thought was to get the distinct count of column2 GROUP BY column1 WHERE distinct count = 1 but that's error city.我的第一个想法是获取 column2 GROUP BY column1 WHERE distinct count = 1 的不同计数,但这是错误的城市。

It's not clearly stated what must be unique?没有明确说明什么必须是独一无二的? If you want to generate unique pairs here you go:如果你想在这里生成独特的对,你 go:

SELECT DISTINCT t1.column2, t2.column2
FROM table1 t1
JOIN table2 t2 ON t1.column1 = t2.column1

Are you trying something like this:你在尝试这样的事情吗:

 select t1.column2,t2.column2 
 from table1 t1 
 inner join 
 table2 t2 on t1.column2= t2.column2 
 where t1.column1 in ('c1value2', 'c1value3')
 group by t1.column2,t2.column2 ;

create table table1  (
  column1 varchar(50), 
  column2 varchar(50) );

insert into table1 values ( 'c1value1', 'c2value1-1'),
                          ( 'c1value2', 'c2value1-2'),
                          ( 'c1value3', 'c2value1-3');

create table table2  (
  column1 varchar(50), 
  column2 varchar(50) );

insert into table2 values ( 'c1value1', 'c2value1-1'),
                          ( 'c1value1', 'c2value2-1'),
                          ( 'c1value1', 'c2value3-1'),
                          ( 'c1value2', 'c2value1-2'),
                          ( 'c1value3', 'c2value1-3');

Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/70演示: 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.编辑:如果您想要基于 table1 的 column1 的两个表的 column2 的唯一值,您可以使用子查询为 column1 选择不同的值并在 where 条件中使用它。

 select t1.column2,t2.column2 
 from table1 t1 
 inner join 
 table2 t2 on t1.column2= t2.column2 
 where t1.column1 in (select distinct column1 from table1)
 group by t1.column2,t2.column2 ;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM