我有3个MySql表,我需要使用一些信息来填充新表。
SELECT table1.products_id as 'ID',
concat(table2.table2_name,':',table2.table2_name,':0') as 'att',
concat(table3.table3_name,':',(@cnt := @cnt + 1)) as 'val'
FROM table1, table2, table3
CROSS JOIN (SELECT @cnt := 0) AS dummy
WHERE table1.options_id=table2.table2_id
AND table1.options_values_id=table3.table3_id
AND table2.language_id=4 AND table3.language_id=4
使用此查询,我获得所有这些(正确的)信息
+++++++++++++++++++++++++++++++++++++
+ ID + att + val +
+++++++++++++++++++++++++++++++++++++
+ 22 + Taglia:Taglia:0 + S:1 +
+ 22 + Taglia:Taglia:0 + M:2 +
+ 22 + Taglia:Taglia:0 + L:3 +
+ 55 + Taglia:Taglia:0 + S:4 +
+ 55 + Taglia:Taglia:0 + M:5 +
+ 60 + Taglia:Taglia:0 + 1:6 +
+ 60 + Taglia:Taglia:0 + 2:7 +
+ 60 + Taglia:Taglia:0 + 3:8 +
+ 62 + Taglia:Taglia:0 + 8,5:9 +
+++++++++++++++++++++++++++++++++++++
但我需要在ID更改时重新启动自动增量值,如下所示:
+++++++++++++++++++++++++++++++++++++
+ ID + att + val +
+++++++++++++++++++++++++++++++++++++
+ 22 + Taglia:Taglia:0 + S:1 +
+ 22 + Taglia:Taglia:0 + M:2 +
+ 22 + Taglia:Taglia:0 + L:3 +
+ 55 + Taglia:Taglia:0 + S:1 +
+ 55 + Taglia:Taglia:0 + M:2 +
+ 60 + Taglia:Taglia:0 + 1:1 +
+ 60 + Taglia:Taglia:0 + 2:2 +
+ 60 + Taglia:Taglia:0 + 3:3 +
+ 62 + Taglia:Taglia:0 + 8,5:1 +
+++++++++++++++++++++++++++++++++++++
我该怎么做?
部分解决了...... @Shadow评论以正确的方式引导我,但我做错了什么:(
NEW QUERY
SET @num := 0, @type := '';
SELECT *
FROM ( select table1.products_id as id, concat(table2.table2_name,':',table2.table2_name,':0') as 'attributo',concat(table3.table3_name,':',@num) as 'valore' FROM table1, table2, table3
WHERE table1.options_id=table2.table2_id AND table1.options_values_id=table3.table3_id AND table2.language_id=4 AND table3.language_id=4
ORDER BY `table1`.`products_id` ASC) as table_name2
WHERE 0 <= GREATEST(
@num := IF(@type = id, @num + 1, 1),
LEAST(0, LENGTH(@type := id)))
新结果
+++++++++++++++++++++++++++++++++++++
+ ID + att + val +
+++++++++++++++++++++++++++++++++++++
+ 22 + Taglia:Taglia:0 + S:2 +
+ 22 + Taglia:Taglia:0 + M:4 +
+ 22 + Taglia:Taglia:0 + L:6 +
+ 55 + Taglia:Taglia:0 + S:2 +
+ 55 + Taglia:Taglia:0 + M:4 +
+ 60 + Taglia:Taglia:0 + 1:2 +
+ 60 + Taglia:Taglia:0 + 2:4 +
+ 60 + Taglia:Taglia:0 + 3:6 +
+ 62 + Taglia:Taglia:0 + 8,5:2 +
+++++++++++++++++++++++++++++++++++++
我哪里错了?