gpt4 book ai didi

mysql:更新出现相同值的列中的行更改值

转载 作者:行者123 更新时间:2023-11-29 21:22:37 26 4
gpt4 key购买 nike

你能帮我准备一个sql,通过将其“meta_value”设置为“meta_key”具有“_wp_attached_file”值和“的行的“post_id”值来更改meta_key具有“_thumbnail_id”值的行的列meta_value中的值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 |

提前非常感谢。

最佳答案

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

以下是demo,根据提供的数据进行验证,结果符合预期。

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)

关于mysql:更新出现相同值的列中的行更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35650456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com