将重复值移动到另一列

时间:2012-02-09 21:26:42

标签: mysql sql database

我有一个表,其中包含基于列id_location的条目对。

"id_image",  "score",  "stddev", "id_image2", "score2", "stddev2", "id_location"
3484,        0.90422,  0.06476,  NULL,        NULL,     NULL,      13
18,          0.71598,  0.06101,  NULL,        NULL,     NULL,      13

如果我想为id_location合并重复项并将它们移到另一列,那么每一行都是唯一的,我该怎么做?

我看了转调和转轴,但那些看起来略有不同。

这将是结果表:

"id_image",  "score",  "stddev", "id_image2", "score2", "stddev2", "id_location"
3484,        0.90422,  0.06476,  18,          0.71598,   0.06101,  13

2 个答案:

答案 0 :(得分:2)

这样做会在第一个插槽中出现较高的id_image

INSERT INTO new_table
SELECT t1.id_image, t1.score, t1.stddev, t2.id_image,
  t2.score, t2.stddev, t1.id_location
FROM old_table t1
JOIN old_table t2
ON t2.id_location = t1.id_location
  AND t2.id_image < t1.id_image

答案 1 :(得分:0)

也许使用子查询:

UPDATE your_table
 SET
  score2 = (SELECT score FROM your_table WHERE id_image = 3484),
  stddev2 = (SELECT stddev FROM your_table WHERE id_image = 3484),
 WHERE id_image = 18;