MySQL:排除重复数据 [英] MySQL: Exclude duplicate data
问题描述
在我的一列中,有重复项,我只想抓住第一次出现的情况.我怎样才能做到这一点?在示例中,我想获取col C中唯一的所有行.因此,我只想要hello ladies
,hello team
,hello cats
和hello sexy
In one of my columns, there's are duplicates and I want to grab the first occurrence only. How can I do that? In the example, I want to grab all the rows that are unique in col C. So I only want hello ladies
, hello team
, hello cats
, and hello sexy
Example Table
---------------
column A | col B | col C
--------------------------
hello | ladies| 1
hello | guys | 1
hello | team | 2
hello | dogs | 2
hello | cats | 3
hello | cats | 3
hello | sexy | 4
推荐答案
DISTINCT
关键字不适用于您的情况.
The DISTINCT
keyword is not applicable in your case.
在DB中,行的顺序是任意的.但是,您可以使用可与字符串一起使用的聚合函数,为每个唯一C值仅选择B列之一.如果字符串的最大值", MAX 就是这样的函数是可以接受的选择:
In the DB the order of rows is arbitrary. You can however select just one of the B column for each unique C value using an aggregate function that can work with strings. MAX is such a function, if the 'maximum' of strings is an acceptable choice:
mysql> select A,max(B),C from Test group by C,A;
+-------+--------+------+
| A | max(B) | C |
+-------+--------+------+
| hello | ladies | 1 |
| hello | team | 2 |
| hello | cats | 3 |
| hello | sexy | 4 |
+-------+--------+------+
4 rows in set (0.00 sec)
这篇关于MySQL:排除重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!