Delete duplicate lines with postgresql conditionals

Asked

Viewed 148 times

1

My code takes information from the internet and inserts it into the database, however, it inserts the same information more than once a day.

I need to leave only 1 record information per day, IE, each day that the program runs, it will enter only 1 time the record in BD.

Example:

    data     |  seller_id  |  
'2017-03-14' |   12345678  |
'2017-03-15' |   12345678  |

1 answer

0


Create a key in the key that you don’t want to repeat, an error will occur if an Insert tries to duplicate, handle the error in the application

Example

ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);

Source : https://www.postgresql.org/docs/9.1/static/sql-altertable.html

  • The problem of creating a key on the date is that more than one id will be entered per day, so I can’t put date as a key. But I can’t put the id either, because each day a new die of that same id will be added. The table would be with various ids and dates: date | seller_id | '2017-03-14' | 12345678 | '2017-03-15' | 12345678 | '2017-03-14' | 87654321 | '2017-03-15' | 87654321 |

  • I spoke of a UNIQUE KEY composed by (date | seller_id) if it is not this did not understand the problem , I’m sorry.

  • Ah yes, now I understand Motta. You’re right! Thanks for the force, I’ll try here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.