A
A
Andrey Kvartsov2018-02-19 11:50:17
MySQL
Andrey Kvartsov, 2018-02-19 11:50:17

How to check if records exist in MySql database?

Good afternoon. Every day I receive a database of IP addresses by URL, of the form:
site.ru
xxx.xx.xxx.x
xxx.xx.xxx.x
site2.ru
xxx.xx.xxx.x
xxx.xx.xxx.x
site3.ru
xxx .xx.xxx.x
xxx.xx.xxx.x
etc.
There are now about 500 such sites, each site has approximately 250 addresses. New addresses and sites appear daily, now I transfer this data to the database:
1. I clear yesterday's version
2. I fill the database again
Is there a more practical way? For example, check which IPs are already in the database, and add only the missing ones?
If so, what is the correct way to execute such queries? because about 250k lines.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Tsvetkov, 2018-02-19
@kolesov_prod

INSERT IGNORE

R
Rsa97, 2018-02-19
@Rsa97

If you need to not only add, but also delete:
First mark all rows as unchanged Then add all entries

INSERT INTO `table` (..., `updated`) 
  VALUES (..., 1) 
  ON DUPLICATE KEY UPDATE `updated` = 1;
At the end, remove the lines that remain unchanged
DELETE FROM `table` WHERE `updated` = 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question