sql >> Databáze >  >> RDS >> Mysql

JSON Vložit do tabulky MySQL nebo aktualizovat, pokud existuje

Následující skript může být užitečný pro váš požadavek:

INSERT INTO `TAG_COUNTER`
  (`account`, `time_id`, `counters`)
VALUES
  ('google', '20180510', '{"gmail_page_viewed": 1, "search_page_viewed": 50}'),
  ('google', '20180510', '{"gmail_page_viewed": 1, "search_page_viewed": 50}'),
  ('google', '20180511', '{"gmail_page_viewed": 1, "search_page_viewed": 100}'),
  ('google', '20180511', '{"gmail_page_viewed": 1, "search_page_viewed": 100}'),
  ('google', '20180511', '{"gmail_page_viewed": 1, "search_page_viewed": 100}')
ON DUPLICATE KEY UPDATE `counters` =
  JSON_SET(`counters`,
           '$."gmail_page_viewed"',
           IFNULL(`counters` ->> '$."gmail_page_viewed"', 0) + 1,
           '$."search_page_viewed"',
           IFNULL(`counters` ->> '$."search_page_viewed"', 0) + 1
  );

Viz dbfiddle .




  1. MySQL ORDER BY DESC je rychlé, ale ASC je velmi pomalé

  2. Funkce COALESCE() v Oracle

  3. Jak mohu importovat geoDataFrame do MySQL?

  4. Jak obnovit databázi Sqlite po zálohování Androidu