根据同一表中的其他行更新行 [英] Updating rows based on other rows in the same table
本文介绍了根据同一表中的其他行更新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下表:项目
+----+------+-------+---------+
| id | type | color | texture |
+----+------+-------+---------+
| 1 | card | red | rough |
| 2 | | red | smooth |
| 3 | | red | rough |
| 4 | | green | rough |
+----+------+-------+---------+
我想更新表中的行,以便如果至少有一行具有非NULL类型的行,并且其他行也与其他属性(颜色和纹理)匹配,它将把值添加到另一行行.因此,在此示例中,它将仅向行id:3
I would like to update rows in the table so that if there is at least one row that has type of not NULL and the other rows also match on the other attributes, color and texture, it will add the value to the other rows. So in this example it would only add 'type' to the row id:3
推荐答案
您可以使用多表UPDATE
语句来做到这一点:
You can use a multiple table UPDATE
statement to do this:
http://dev.mysql.com/doc/refman/5.0/en/update.html
UPDATE items i1
JOIN items i2
ON i1.color = i2.color
AND i1.texture = i2.texture
AND i2.type IS NOT NULL
SET i1.type = i2.type
WHERE i1.type IS NULL
这篇关于根据同一表中的其他行更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文