P
P
Pashenka2014-05-21 00:08:01
MySQL
Pashenka, 2014-05-21 00:08:01

How to use ON DUPLICATE KEY UPDATE?

Tell me how to rename the values ​​​​when they match in the slug cell? Ideally, add a digit with a hyphen. If slug exists, then trace. will be slug-2 if there is such a slug-3, and so on.
what I sketched does not update - swears at a double

CREATE TABLE IF NOT EXISTS `table` (
  `slug` varchar(200) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `slug` (`slug`),
  KEY `name` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO `wc_terms` (`id`, `name`, `slug`) VALUES
(1, 'value', 'value'),
(2, 'value', 'value'),
(3, 'value', 'value')
ON DUPLICATE KEY UPDATE slug = slug + 1;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2014-05-21
@0neS

INSERT INTO wc_terms (id, name, slug) VALUES
(1, 'value', 'value'),
(2, 'value', 'value'),
(3, 'value', 'value')
ON DUPLICATE KEY UPDATE slug = VALUES(slug) + 1;

stackoverflow.com/questions/6889289/sql-on-duplica...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question