gpt4 book ai didi

mysql - 连接具有相同列的两个表的结果避免重复

转载 作者:行者123 更新时间:2023-11-29 10:27:08 25 4
gpt4 key购买 nike

我有两个包含不同数据的表,我想连接结果以便从两个表中获取数据以避免重复。

表1:

Seasons  | Colors
win 17 | grey
win 17 | blue
win 18 | red
win 18 | black

表2:

Seasons  | Sizes | Weights
win 17 | 48 | 8
win 17 | 47 | 7
win 17 | 48 | 5
win 17 | 47 | 9
win 18 | 42 | 2
win 18 | 43 | 3
win 18 | 42 | 4
win 18 | 43 | 1

加入类似:

Seasons  | Sizes | Weights | Colors
win 17 | 48 | 8 | grey
win 17 | 47 | 7 | grey
win 17 | 48 | 5 | grey
win 17 | 47 | 9 | grey
win 17 | 48 | 8 | blue
win 17 | 47 | 7 | blue
win 17 | 48 | 5 | blue
win 17 | 47 | 9 | blue
win 18 | 42 | 2 | red
win 18 | 43 | 3 | red
win 18 | 42 | 4 | red
win 18 | 43 | 1 | red
win 18 | 42 | 2 | black
win 18 | 43 | 3 | black
win 18 | 42 | 4 | black
win 18 | 43 | 1 | black

我已经尝试过

SELECT
Seasons,
Sizes,
Weights,
Colors
FROM (SELECT
Seasons,
Colors
FROM Table1) c
CROSS JOIN (SELECT
Sizes,
Weights
FROM table2) s
WHERE c.Seasons = s.Seasons
GROUP BY Seasons,
Colors,
Sizes,
Weights;

但是我在同一季节得到了不同尺寸和代码的重复颜色

最佳答案

尝试一下:您可以尝试使用 JOINDISTINCT 但我不明白为什么您要以这样的方式存储数据以更好的方式将其标准化。

SELECT DISTINCT t1.Seasons, 
t2.Sizes,
t2.Weights,
t1.Colors
FROM table1 t1
INNER JOIN table2 t2 ON t1.Seasons = t2.Seasons

关于mysql - 连接具有相同列的两个表的结果避免重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48093384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com