简体   繁体   中英

How to select just unique values into a mysql table based on the value in one column?

I have table named x

Here is the sample data in table x

Column1_ _ __ _ __ _ __ _ _ Column2_ _ __ _ __ _ __ _ ___ Column3

a _ __ _ __ _ __ _ __ _ __ _ ___ b _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c

a2_ _ __ _ __ _ __ _ __ _ __ _ _ b2 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c

a3_ _ __ _ __ _ __ _ __ _ __ _ _ b3 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c2

a4_ _ __ _ __ _ __ _ __ _ __ _ b4 _ __ _ __ _ __ _ __ _ __ _ __ _ ___ _c3

The result I need in a new table is:

Column1_ _ __ _ __ _ __ _ _ Column2_ _ __ _ __ _ __ _ ___ Column3

a3_ _ __ _ __ _ __ _ __ _ __ _ _ b3 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c2

a4_ _ __ _ __ _ __ _ __ _ __ _ b4 _ __ _ __ _ __ _ __ _ __ _ __ _ ___ _c3

I tried importing the values to a new table and applying a unique index on column 3 but the result i get out of it is

Column1_ _ __ _ __ _ __ _ _ Column2_ _ __ _ __ _ __ _ ___ Column3

a_ _ __ _ __ _ __ _ __ _ __ _ _ b _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c

a3_ _ __ _ __ _ __ _ __ _ __ _ _ b3 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _c2

a4_ _ __ _ __ _ __ _ __ _ __ _ b4 _ __ _ __ _ __ _ __ _ __ _ __ _ ___ _c3

SELECT * FROM x WHERE Column3 NOT IN 
  (SELECT Column3 FROM x GROUP BY Column3 HAVING COUNT(*) > 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM