我试图根据另一个表中的值更新一列中的值。
如果列都在同一个表中,我可以这样做:
UPDATE wp_posts
SET post_date = REPLACE (post_date, 'X', 'Y')
WHERE meta_value = 'Z';
但我需要先加入两个表才能更新:
SELECT *
FROM wp_posts
LEFT OUTER JOIN wp_postmeta ON post_id = post_id
如何根据我加入的两个表进行更新?
我试图根据另一个表中的值更新一列中的值。
如果列都在同一个表中,我可以这样做:
UPDATE wp_posts
SET post_date = REPLACE (post_date, 'X', 'Y')
WHERE meta_value = 'Z';
但我需要先加入两个表才能更新:
SELECT *
FROM wp_posts
LEFT OUTER JOIN wp_postmeta ON post_id = post_id
如何根据我加入的两个表进行更新?
试试这种方式,如果你使用SQL Server
UPDATE WP
SET WP.post_date = REPLACE(WPM.post_date, 'X', 'Y')
FROM wp_posts WP
LEFT OUTER JOIN wp_postmeta WPM ON WP.post_id = WPM.post_id
WHERE WP.meta_value = 'Z';
更新:
如果您使用的是MySQL。这是MySQL的更新
UPDATE wp_posts WP
LEFT JOIN wp_postmeta WPM ON WP.post_id = WPM.post_id
SET WP.post_date = REPLACE(WPM.post_date, 'X', 'Y')
WHERE meta_value = 'Z';
SQL 服务器
UPDATE wp_posts
post_date = REPLACE (post_date, 'X', 'Y')
FROM wp_posts WP LEFT OUTER JOIN wp_postmeta WPM ON WP.post_id = WPM.post_id
WHERE meta_value = 'Z';
MySQL
UPDATE wp_posts
LEFT JOIN wp_postmeta WPM ON WP.post_id = WPM.post_id
SET post_date = REPLACE (post_date, 'X', 'Y')
WHERE meta_value = 'Z'
UPDATE wp_posts SET post_date = REPLACE (WPM.post_date, 'X', 'Y')
FROM wp_posts AS WP
LEFT OUTER JOIN wp_postmeta AS WPM ON WP.post_id = WPM.post_id
WHERE WP.meta_value = 'Z'