【发布时间】:2012-05-07 13:07:52
【问题描述】:
我有一个 MySQL GROUP_CONCAT .... INSERT 问题 此代码有效:
SELECT group_concat(tabel2.img_name separator ',')
FROM tabel2
GROUP BY tabel2.produit_id
我需要将结果插入到另一个表中,但我卡住了。
这个(或我能想到的任何组合)都行不通
INSERT INTO tabel1.imgname
SELECT group_concat(tabel2.img_name separator ',')
FROM tabel2
GROUP BY tabel2.produit_id
WHERE tabel1.product_id = tabel2.produit_id
我做错了什么?
CREATE TABLE IF NOT EXISTS `tabel1` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`rubrique_id` int(11) NOT NULL,
`marque_id` int(11) NOT NULL,
`subfamily_id` int(11) NOT NULL,
`product_name` varchar(150) NOT NULL,
`imgname` varchar(255) DEFAULT NULL,
`product_description1` text NOT NULL,
`product_description2` text NOT NULL,
`product_order` int(11) NOT NULL,
`product_page` int(11) NOT NULL,
`price_min` float NOT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;
INSERT INTO `tabel1` (`product_id`, `rubrique_id`, `marque_id`, `subfamily_id`, `product_name`, `imgname`, `product_description1`, `product_description2`, `product_order`, `product_page`, `price_min`)
VALUES
(33, 15, 23, 40, 'product 1', NULL, '', '', 0, 0, 0),
(34, 13, 13, 13, 'product 2', NULL, '', '', 0, 0, 0),
(35, 14, 14, 14, 'product 3', NULL, '', '', 0, 0, 0);
CREATE TABLE IF NOT EXISTS `tabel2` (
`img_id` int(11) NOT NULL AUTO_INCREMENT,
`img_name` text NOT NULL,
`article_id` int(11) DEFAULT NULL,
`produit_id` int(11) DEFAULT NULL,
`product_select` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`img_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4018 ;
INSERT INTO `tabel2` (`img_id`, `img_name`, `article_id`, `produit_id`, `product_select`)
VALUES
(4013, 'acoacki086050.jpg', 13342, 33, NULL),
(4014, '32252il6jh2dqex.jpg', NULL, 34, NULL),
(4015, '33265ulrzmgr18w.jpg', NULL, 34, NULL),
(4016, '40334zcfk0c4n67.jpg', NULL, 35, NULL),
(4017, '40473frd4900u82.jpg', NULL, 35, NULL);
所以说 tabel1.product_id = 34 我需要有 tabel1.imgname = 32252il6jh2dqex.jpg,33265ulrzmgr18w.jpg
【问题讨论】:
-
请提供 tabel1 和 tabel2 的完整架构,以及您希望查询执行的操作的说明。
-
我刚才看到了评论,我已经用上述表格和我需要做的事情更新了我的帖子。更糟糕的是,我会将 concat 导出为 csv,然后仅从 csv 文件更新该列。但我非常不想这样做,特别是因为桌子真的很大
-
你不想要
INSERT,你可能想要UPDATE -
只要能完成工作,我将使用什么并不重要。 UPDATE也没有用,我试过了。
-
感谢架构和要求。这很有帮助。我已经通过回答更新了一个适合您的查询。
标签: mysql insert group-concat insert-update