Thursday 29 May 2014

Multiple Row Updates in MySQL

CREATE TABLE  `testimonial_setting` (
  `testimonial_setting_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `testimonial_label` varchar(255) NOT NULL,
  `testimonial_key` varchar(45) NOT NULL,
  `testimonial_value` varchar(45) NOT NULL,
  PRIMARY KEY (`testimonial_setting_id`)
);

Table :-  testimonial_setting
testimonial_setting_id, testimonial_label,    testimonial_key, testimonial_value
            1,                       'Background Color',    'bgcolor',            ''
            2,                        'Text Color',               'textcolor',          ''
            3,                      'Heding Text Color',    'headtextcolor',    ''
            4,               'Heding Background Color', 'headbgcolor',    ''

Query :-

UPDATE `testimonial_setting` SET
`testimonial_value` = IF(`testimonial_key`="bgcolor","#ff00ff",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="textcolor","#ffffff",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="headtextcolor","#ff0000",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="headbgcolor","#ff0fff",`testimonial_value`);

After update table :-  testimonial_setting


testimonial_setting_id, testimonial_label,    testimonial_key, testimonial_value
            1,                       'Background Color',    'bgcolor',            '#ff00ff'
            2,                        'Text Color',               'textcolor',          '#ffffff'
            3,                      'Heding Text Color',    'headtextcolor',    '#ff0000'
            4,               'Heding Background Color', 'headbgcolor',    '#ff0fff'