@MasterCopipaster

Как удалить дубликаты из базы данных когда в колонках есть NULL?

Господа помогите, есть такая таблица с кучей дубликатов
id|article |brand         |title|brand_id|product_id|analog_product_id|
--|--------|--------------|-----|--------|----------|-----------------|
 1|96397201|General Motors|     |     277|    422577|                 |
 2|96397201|General Motors|     |     277|    422577|                 |
 3|96397201|General Motors|     |     277|    422577|                 |
 4|96397201|General Motors|     |     277|    422577|                 |
 5|96397201|General Motors|     |     277|    422577|           423053|
 6|96397201|General Motors|     |     277|    422577|                 |
 7|96397201|General Motors|     |     277|    422577|                 |
 8|96397201|General Motors|     |     277|    422577|                 |
 9|96397201|General Motors|     |     277|    422577|           424646|
10|96397201|General Motors|     |     277|    422577|                 |
11|96397201|General Motors|     |     277|    422577|                 |
12|96397201|General Motors|     |     277|    422577|                 |
13|96397201|General Motors|     |     277|    422577|                 |
14|96397201|General Motors|     |     277|    422577|                 |
15|96397201|General Motors|     |     277|    422577|                 |
16|96397201|General Motors|     |     277|    422577|                 |
17|96397201|General Motors|     |     277|    422577|                 |
18|96397201|General Motors|     |     277|    422577|                 |
19|96397201|General Motors|     |     277|    422577|                 |
20|96397201|General Motors|     |     277|    422577|                 |
21|96397201|General Motors|     |     277|    422577|                 |
22|96397201|General Motors|     |     277|    422577|                 |
23|96397201|General Motors|     |     277|    422577|                 |
24|96397201|General Motors|     |     277|    422577|                 |
25|96397201|General Motors|     |     277|    422577|                 |
26|96397201|General Motors|     |     277|    422577|                 |
27|96397201|General Motors|     |     277|    422577|                 |
28|96397201|General Motors|     |     277|    422577|                 |
29|96397201|General Motors|     |     277|    422577|           422519|
30|96397201|General Motors|     |     277|    422577|                 |
31|96397201|General Motors|     |     277|    422577|                 |
32|96397201|General Motors|     |     277|    422577|           421499|
33|96397201|General Motors|     |     277|    422577|                 |
34|96397201|General Motors|     |     277|    422577|                 |
35|96397201|General Motors|     |     277|    422577|                 |
36|96397201|General Motors|     |     277|    422577|           420967|
37|96397201|General Motors|     |     277|    422577|                 |
38|96397201|General Motors|     |     277|    422577|                 |
39|96397201|General Motors|     |     277|    422577|                 |
40|96397201|General Motors|     |     277|    422577|                 |
41|96397201|General Motors|     |     277|    422577|                 |
42|96397201|General Motors|     |     277|    422577|                 |
43|96397201|General Motors|     |     277|    422577|                 |
44|96397201|General Motors|     |     277|    422577|                 |
45|96397201|General Motors|     |     277|    422577|                 |
46|96397201|General Motors|     |     277|    422577|                 |
47|96397201|General Motors|     |     277|    422577|                 |
48|96397201|General Motors|     |     277|    422577|                 |
49|96397201|General Motors|     |     277|    422577|           423366|
50|96397201|General Motors|     |     277|    422577|                 |
51|96397201|General Motors|     |     277|    422577|                 |
52|96397201|General Motors|     |     277|    422577|                 |
53|96397201|General Motors|     |     277|    422577|                 |
54|96397201|General Motors|     |     277|    422577|                 |
55|96397201|General Motors|     |     277|    422577|                 |
56|96397201|General Motors|     |     277|    422577|                 |
57|96397201|General Motors|     |     277|    422577|                 |
58|96397201|General Motors|     |     277|    422577|                 |
59|96397201|General Motors|     |     277|    422577|                 |
60|96397201|General Motors|     |     277|    422577|                 |
61|96397201|General Motors|     |     277|    422577|                 |
62|96397201|General Motors|     |     277|    422577|           424210|
63|96397201|General Motors|     |     277|    422577|                 |
64|96397201|General Motors|     |     277|    422577|                 |
65|96397201|General Motors|     |     277|    422577|                 |
66|96397201|General Motors|     |     277|    422577|                 |
67|96397201|General Motors|     |     277|    422577|                 |
68|96397201|General Motors|     |     277|    422577|                 |
69|96397201|General Motors|     |     277|    422577|                 |
70|96397201|General Motors|     |     277|    422577|                 |
71|96397201|General Motors|     |     277|    422577|                 |
72|96397201|General Motors|     |     277|    422577|                 |
73|96397201|General Motors|     |     277|    422577|                 |
74|96397201|General Motors|     |     277|    422577|                 |
75|96397201|General Motors|     |     277|    422577|                 |
76|96397201|General Motors|     |     277|    422577|                 |
77|96397201|General Motors|     |     277|    422577|                 |
78|96397201|General Motors|     |     277|    422577|                 |
79|96397201|General Motors|     |     277|    422577|                 |
80|96397201|General Motors|     |     277|    422577|                 |
81|96397201|General Motors|     |     277|    422577|                 |


Я раньше что бы удалить дубликаты делал примерно так
DELETE t1 FROM table_name t1 INNER JOIN table_name t2  WHERE t1.id < t2.id AND t1.article = t2.article AND t1.brand = t2.brand AND t1.brand_id = t2.brand_id and t1.product_id

Это отлично работает если в колонках нету NULL, а вот если есть то не работает :(

Я попробывал сделать так два запроса для удаления дубликатов
DELETE t1 FROM table_name t1 INNER JOIN table_name t2  WHERE t1.id < t2.id AND t1.article = t2.article AND t1.brand = t2.brand AND t1.brand_id = t2.brand_id and t1.product_id  is not null 

DELETE t1 FROM table_name t1 INNER JOIN table_name t2  WHERE t1.id < t2.id AND t1.article = t2.article AND t1.brand = t2.brand AND t1.brand_id = t2.brand_id and t1.analog_product_id is not null


Но тогда у меня в результате остается всего одна запись
id|article |brand         |title|brand_id|product_id|analog_product_id|
--|--------|--------------|-----|--------|----------|-----------------|
81|96397201|General Motors|     |     277|    422577|                 |


А должно получатся как то так

id|article |brand         |title|brand_id|product_id|analog_product_id|
--|--------|--------------|-----|--------|----------|-----------------|
 5|96397201|General Motors|     |     277|    422577|           423053|
 9|96397201|General Motors|     |     277|    422577|           424646|
29|96397201|General Motors|     |     277|    422577|           422519|
32|96397201|General Motors|     |     277|    422577|           421499|
36|96397201|General Motors|     |     277|    422577|           420967|
49|96397201|General Motors|     |     277|    422577|           423366|
62|96397201|General Motors|     |     277|    422577|           424210|
81|96397201|General Motors|     |     277|    422577|                 |


Подскажите пожалуйста как я должен изменить запрос? я так понимаю делать в два запроса это не правельно, потому что первый удаляет все что не с null а второй все что с null и остается одна запись...

Подскажите как добится удаление дубликатов что бы результат был как в примере ниже?
  • Вопрос задан
  • 60 просмотров
Решения вопроса 1
Для каждого nullable поля условие должно будет выглядить примерно так:

... AND (table1.field1 = table2.field1 OR table1.field1 IS NULL AND table2.field1 IS NULL) AND ...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы