Chris
Chris

Reputation: 672

Select without duplicate values in one column

My construction:

INSERT INTO ... ( SELECT ... FROM .. JOIN ... ON ... WHERE ... ORDER BY ... )

Short description: I'm using Select to insert data from 2 tables (using JOIN) to another table. The result of Select is something like (after order by):

col_a  |  col_b  |  col_c |        col_d

1111       44         xxx            yyy
1111       66         xxx            yyy
2222       12         aaa            bbb
3333       55         ccc            ddd
3333       68         xxx            yyy

If the row is duplicated (col_c) i want to insert to my table first matched row.

Example of result should be like this:

col_a  |  col_b  |  col_c |        col_d

1111       44         xxx            yyy
2222       12         aaa            bbb
3333       55         ccc            ddd

Upvotes: 1

Views: 333

Answers (2)

Chris
Chris

Reputation: 672

I think I found a solution. I create unique key for the col in table. After that INSERT IGNORE is everyhing what I need :)

Upvotes: 0

Ramie
Ramie

Reputation: 1201

Use the GROUP BY function

(SELECT ... FROM .. JOIN ... ON ... WHERE ... ORDER BY ... GROUP BY [col_c])

Upvotes: 2

Related Questions