基于以下MYSQL查询,如何编写UPDATE语句来更新值?

时间:2014-10-10 13:39:32

标签: mysql wordpress

我使用以下SQL查询从Wordpress数据库中获取所需的记录。

SELECT p.id, p.post_title, m.meta_key, m.meta_value, t.slug
FROM wp_posts p
INNER JOIN wp_postmeta m ON p.id=m.post_id 
AND m.meta_key='_subscription_sign_up_fee'
INNER JOIN wp_term_relationships AS tr ON p.id = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id    
WHERE t.slug = 'bundle'

这会拉出正确的记录。我的问题是我需要将_subscription_sign_up_fee的值增加100。

我已尝试过以下

update wp_postmeta
set wp_postmeta.meta_value = wp_postmeta.meta_value + 100 
INNER JOIN wp_postmeta m ON wp_posts.id=m.post_id 
AND m.meta_key='_subscription_sign_up_fee'
INNER JOIN wp_term_relationships AS tr ON wp_posts.id = tr.object_id
INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id   
WHERE t.slug = 'bundle'

并收到错误消息,说明'列' meta_value'在字段列表中是模糊的' 我很确定我不理解别名。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

MySQL UPDATE JOIN的正确语法是在结尾处设置SET。你也有一些问题,你加入wp_postmeta两次,但是用wp_posts丢失了连接。

这样的事情应该有用;

UPDATE wp_postmeta m
JOIN wp_posts 
  ON wp_posts.id = m.post_id AND m.meta_key = '_subscription_sign_up_fee'
JOIN wp_term_relationships AS tr 
  ON wp_posts.id = tr.object_id
JOIN wp_term_taxonomy AS tt 
  ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t 
  ON tt.term_id = t.term_id   
SET m.meta_value = m.meta_value + 100 
WHERE t.slug = 'bundle'

...并且始终记得在从互联网上的随机人员运行潜在的破坏性操作之前备份您的数据:)