简体   繁体   English

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

[英]Based on the following MYSQL query how can I write an UPDATE statement to update a value?

I'm using the following SQL query to get the records I need from a Wordpress database. 我正在使用以下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'

This pulls the correct records. 这将提取正确的记录。 My issue is that I need to increase the value of the _subscription_sign_up_fee by 100. 我的问题是我需要将_subscription_sign_up_fee的值增加100。

I've tried the following 我尝试了以下

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'

and get an error saying that 'Column 'meta_value' in field list is ambiguous ' I'm pretty sure I'm not understanding the aliases. 并收到一条错误消息,指出“字段列表中的列'meta_value'是不明确的'我很确定我不理解别名。 Can someone point me in the right direction? 有人可以指出我正确的方向吗?

The correct syntax for MySQL UPDATE JOINs is to have SET at the end. MySQL UPDATE JOIN的正确语法是在末尾加上SET。 You also have some problems in that you're joining wp_postmeta twice, but lost the join with wp_posts. 您还会遇到一些问题,因为您要两次加入wp_postmeta,但是由于wp_posts而失去了加入。

Something like this should work; 这样的事情应该起作用;

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'

...and always remember to back up your data before running potentially destructive operations from random people on the Internet :) ...并且始终记得在运行Internet上的随机操作可能造成破坏性的操作之前备份数据:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM