Compare SQL to PHP arrays

Asked 2 years ago, Updated 2 years ago, 46 views

Programming with PHP and MySql

The $data array contains id (unique), date.
Insert this data into the MySQL table

If MySQL already has the same date, you want to exclude that $data and insert the data into MySQL.

Please let me know if there is a simple way to compare the array with the MySQL table data.

php mysql sql

2022-09-30 14:41

2 Answers

You can create and update with the first key at the same time.

INSERT INTO table(a,b,c)VALUES(1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

https://dev.mysql.com/doc/refman/5.6/ja/insert-on-duplicate.html

If it already exists, you can update the update item with a date, etc., or overwrite the existing data.If there is no data update, it is also fast because it does not actually run.


2022-09-30 14:41

I think it's easiest to select data and then compare it.

select date from table_name;

I will insert it after I get it as an array.

If you use insert into ... duplicate key update, it is easier to update the data by setting the update target to an update date or something like that.


2022-09-30 14:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.