我正在尝试过滤掉 color 列的重复项,但仅限于每组 parent_id,而不是整个颜色列本身。
例如,我有下表:
parent_id | child_id | color |
5 | 1 | blue |
5 | 2 | blue |
5 | 3 | green |
5 | 4 | green |
5 | 5 | yellow |
5 | 6 | orange |
6 | 7 | blue |
6 | 8 | blue |
6 | 9 | magenta |
6 | 10 | green |
6 | 11 | magenta |
6 | 12 | orange |
我正在寻找的结果是这样的:
parent_id | child_id | color |
5 | 1 | blue |
5 | 3 | green |
5 | 5 | yellow |
5 | 6 | orange |
6 | 7 | blue |
6 | 9 | magenta |
6 | 10 | green |
6 | 12 | orange |
请注意,parent_id = 5
只有一种蓝色,而 parent_id = 6
则只有一种蓝色。其余颜色也是如此。
非常感谢对此的任何帮助。
最佳答案
select parent_id, min(child_id), color
from your_tab
group by parent_id, color;
关于mysql - 根据 SQL 中的组过滤掉重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26981895/