我有一个看起来像这样的表:
entity_guid | name | value
---------------|----------------|--------
189501 | context | groups
189501 | column | 3
189509 | context | profile
189509 | column | 3
189521 | context | profile
189521 | column | 2
189551 | context | groups
189551 | column | 1
189552 | context | groups
189552 | column | 3
189554 | context | groups
189554 | column | 2
189559 | context | profile
189559 | column | 1
189591 | context | profile
189591 | column | 3
我想要更新name = column
和value = 3
的行,以便value = 2
。但我只想更新另一行存在的行entity_guid
以及name = context
和value = groups
。
例如,如果我要选择要更改的行,它们将是:
entity_guid | name | value
---------------|----------------|--------
189501 | column | 3
189552 | column | 3
我试过了:
SELECT * FROM (
SELECT * FROM `elggprivate_settings` WHERE `name` = 'context' AND `value` = 'groups'
) AS subquery
WHERE `name` = 'column' AND `value` = 3
但那当然不起作用,因为表没有像那样设置;同一entity_guid
有多行。顺便说一下,这是社交网络引擎elgg的数据库中的表。我对MySQL不是很好,也无法在线找到答案。
答案 0 :(得分:1)
xQbert很接近,但他忘记了用于检查子记录存在的context / groups子句。
UPDATE
elggprivate_settings
SET
value = '2'
WHERE
name = 'column' AND value = '3'
AND entity_guid IN (
SELECT
entity_guid
FROM
elggprivate_settings
WHERE
name = 'context'
AND value = 'groups')
答案 1 :(得分:0)
最快的方法是将表连接到自身(连接通常比子查询更快):
UPDATE elggprivate_settings es
JOIN elggprivate_settings es2
ON es2.entity_guid = es.entity_guid
SET es.value = '2'
WHERE es.value = '3'
AND es.name = 'column'
AND es2.name = 'context'
and es2.value = 'groups';