1

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
Chris
  • 652
  • 1
  • 7
  • 22
  • 1
    Please add the query like you already have.... on which column do you want to order by? And you only want to check on duplicate in col_c? – arnoudhgz Feb 11 '15 at 19:30
  • Use insert .. on duplicate like explained [here](http://stackoverflow.com/questions/13041023/insert-on-duplicate-key-update-nothing-using-mysql/13041065#13041065) – A.D. Feb 11 '15 at 19:32

2 Answers2

2

Use the GROUP BY function

(SELECT ... FROM .. JOIN ... ON ... WHERE ... ORDER BY ... GROUP BY [col_c])
Ramie
  • 1,171
  • 2
  • 16
  • 35
  • GROUP BY is executing before ORDER BY .. I want to first ORDER my result and then eliminate duplicates – Chris Feb 11 '15 at 19:37
0

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

Chris
  • 652
  • 1
  • 7
  • 22