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
2
  • 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? Commented Feb 11, 2015 at 19:30
  • Use insert .. on duplicate like explained here Commented Feb 11, 2015 at 19:32

2 Answers 2

2

Use the GROUP BY function

(SELECT ... FROM .. JOIN ... ON ... WHERE ... ORDER BY ... GROUP BY [col_c])
Sign up to request clarification or add additional context in comments.

1 Comment

GROUP BY is executing before ORDER BY .. I want to first ORDER my result and then eliminate duplicates
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.