mysql:在发生相同值的列中更新行更改值

时间:2016-02-26 11:24:57

标签: mysql sql

你能帮我准备一个sql,它可以改变meta_key所拥有的行的meta_value列中的值" _thumbnail_id"通过设置" meta_value" to" post_id"行的值,其中" meta_key"有" _wp_attached_file"价值和" meta_value"具有相同的图像名称。 它有点复杂,所以我给你举个例子。我想改变这个:

+---------+---------+------------------+--------------+
| meta_id | post_id | meta_key         | meta_value   |
+---------+---------+------------------+--------------+
| 312     | 170     | _wp_attached_file| IMGP0289.jpg | 
| 313     | 54      | _thumbnail_id    | IMGP0289.jpg | 
| 314     | 169     | _wp_attached_file| IMGP0453.jpg | 
| 315     | 53      | _thumbnail_id    | IMGP0453.jpg | 

到此:

+---------+---------+------------------+--------------+
| meta_id | post_id | meta_key         | meta_value   |
+---------+---------+------------------+--------------+
| 312     | 170     | _wp_attached_file| IMGP0289.jpg | 
| 313     | 54      | _thumbnail_id    | 170          | 
| 314     | 169     | _wp_attached_file| IMGP0453.jpg | 
| 315     | 53      | _thumbnail_id    | 169          |

非常感谢。

2 个答案:

答案 0 :(得分:0)

这是一个带有临时表克隆辅助的解决方案。

以下是演示,并使用提供的数据进行验证,结果符合预期。

SQL:

-- data preparation
create table tbl(meta_id int, post_id int, meta_key char(100), meta_value char(100));
insert into tbl values
(312     , 170     , '_wp_attached_file', 'IMGP0289.jpg'),
(313     , 54      , '_thumbnail_id'    , 'IMGP0289.jpg'),
(314     , 169     , '_wp_attached_file', 'IMGP0453.jpg'),
(315     , 53      , '_thumbnail_id'    , 'IMGP0453.jpg');
SELECT * FROM tbl;

-- query needed
CREATE TEMPORARY TABLE tbl_tmp LIKE tbl;
INSERT INTO tbl_tmp SELECT * FROM tbl;

UPDATE
    tbl t1
SET 
    meta_value = (SELECT 
                        post_id
                    FROM
                        tbl_tmp t2
                    WHERE
                        t1.meta_value = t2.meta_value AND
                        t2.meta_key = '_wp_attached_file'
                    LIMIT 1
                    )
WHERE 
    t1.meta_key = '_thumbnail_id';

SELECT * FROM tbl;

输出:

mysql> SELECT * FROM tbl;
+---------+---------+-------------------+--------------+
| meta_id | post_id | meta_key          | meta_value   |
+---------+---------+-------------------+--------------+
|     312 |     170 | _wp_attached_file | IMGP0289.jpg |
|     313 |      54 | _thumbnail_id     | IMGP0289.jpg |
|     314 |     169 | _wp_attached_file | IMGP0453.jpg |
|     315 |      53 | _thumbnail_id     | IMGP0453.jpg |
+---------+---------+-------------------+--------------+
4 rows in set (0.00 sec)

mysql> -- query needed
mysql> CREATE TEMPORARY TABLE tbl_tmp LIKE tbl;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO tbl_tmp SELECT * FROM tbl;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> UPDATE
    ->     tbl t1
    -> SET
    ->     meta_value = (SELECT
    ->                         post_id
    ->                     FROM
    ->                         tbl_tmp t2
    ->                     WHERE
    ->                         t1.meta_value = t2.meta_value AND
    ->                         t2.meta_key = '_wp_attached_file'
    ->                     LIMIT 1
    ->                     )
    -> WHERE
    ->     t1.meta_key = '_thumbnail_id';
 FROM Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> SELECT * FROM tbl;
+---------+---------+-------------------+--------------+
| meta_id | post_id | meta_key          | meta_value   |
+---------+---------+-------------------+--------------+
|     312 |     170 | _wp_attached_file | IMGP0289.jpg |
|     313 |      54 | _thumbnail_id     | 170          |
|     314 |     169 | _wp_attached_file | IMGP0453.jpg |
|     315 |      53 | _thumbnail_id     | 169          |
+---------+---------+-------------------+--------------+
4 rows in set (0.00 sec)

答案 1 :(得分:0)

怎么样......

UPDATE tbl x 
  JOIN tbl y
    ON y.meta_value = x.meta_value
   SET x.meta_value = y.post_id
 WHERE x.meta_key = '_thumbnail_id' 
   AND y.meta_key = '_wp_attached_file';

http://www.sqlfiddle.com/#!9/da9a66/1