Eléphant du PHP |
217 Messages
27 mai 2010, 17:14
Bonjour,
vous pouvez utiliser une jointure dans votre update comme une jointure de requete select. Cette jointure n'est pas obligé de se référer au champ a mettre à jour.
Exemple sur la structure suivante :
CREATE TABLE IF NOT EXISTS `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Contenu de la table `table1`
--
INSERT INTO `table1` (`id`, `description`) VALUES
(1, 'description table 1');
CREATE TABLE IF NOT EXISTS `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_table1` int(11) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_table1` (`id_table1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `table2` (`id`, `id_table1`, `description`) VALUES
(1, 1, 'description table 2');
La jointure se fera sur table1.id=table2.id_table1 :
UPDATE
table1,table2
SET
table1.description = "new desc",
table2.description = "new desc"
WHERE
table1.id =1
AND
table1.id = table2.id_table1;
update table1 INNER JOIN table2 ON(table1.id=table2.id_table1)
set
table1.description="description inner join",
table2.description="description inner join"
WHERE
table1.id=1