删除MySQL中的重复组合

时间:2017-12-12 14:31:13

标签: mysql sql duplicates combinations

我的问题与此问题类似:https://stackoverflow.com/a/7517904
我有4个表,我用键A和B链接:

A

private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
    TextBlock tb = sender as TextBlock;
    Border border = tb.Parent as Border;
    DockPanel dockPanel = border.Parent as DockPanel;
    AdornedElementPlaceholder aph = dockPanel.Children[1] as AdornedElementPlaceholder;
    FrameworkElement adornedElement = aph.AdornedElement as FrameworkElement;
    adornedElement.Margin = new Thickness(0, 0, tb.ActualWidth + 10.0, 0);
}

A B C 
1 a c 

C

A B D 
1 a d 
1 a e 

d

A B E 
1 a h 
1 a i 

我想一起加入表格,结果应该是:

A B F 
1 a k 
1 a l 

我的实际结果如下:

A B C D E F
1 a c d h k 
1 a c e i l 

问题是,我得到了所有组合。我希望尽可能减少行数。我尝试了另一个线程的解决方案,使用rownumbers:

A B C D E F 
1 a c d h k
1 a c e h k
1 a c d h l
1 a c e h l 
1 a c e i l
1 a c d i l
1 a c d i k 
1 a c e i k

我希望,我把问题描述得很清楚。

1 个答案:

答案 0 :(得分:0)

你在说什么,我认为是你想要的    A连接到B的第一行,C的第一行和D的第一行,和    A连接到第二行B,第二行C和第二行D

关系数据库在行中没有任何隐含的序列;如果你想要一个序列,它必须是一个列值。这有很好的理由,但这里解释的时间太长了。结果是您需要一个序列列,如下所示:

create table a (a integer, b varchar(1), c varchar(1));
insert into a (a, b, c) values (1, 'a', 'c');

create table b (a integer, b varchar(1), sequence integer, d varchar(1));
insert into b (a, b, sequence, d) values (1, 'a', 1, 'd');
insert into b (a, b, sequence, d) values (1, 'a', 2, 'e');

create table c (a integer, b varchar(1), sequence integer, e varchar(1));
insert into c (a, b, sequence, e) values (1, 'a', 1, 'h');
insert into c (a, b, sequence, e) values (1, 'a', 2, 'i');

create table d (a integer, b varchar(1), sequence integer, f varchar(1));
insert into d (a, b, sequence, f) values (1, 'a', 1, 'k');
insert into d (a, b, sequence, f) values (1, 'a', 2, 'l');

然后查询是:

select distinct
    a.a,
    a.b,
    a.c,
    b.d,
    c.e,
    d.f
from 
    a 
    join 
    b on a.a = b.a and a.b = b.b 
    join 
    c on a.a = c.a and a.b = c.b and c.sequence = b.sequence
    join 
    d on a.a = d.a and a.b = d.b and d.sequence = b.sequence;

结果是:

+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
| 1 | a | c | d | h | k |
| 1 | a | c | e | i | l |
+---+---+---+---+---+---+
2 rows in set (0.00 sec)