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
解决方案
The DISTINCT keyword is not applicable in your case.
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)