开发者

Update MySQL column if value in one of two column matches

I have a table with survey data and another with data about each respondent to the survey. One table (data) has 3 fields "name", "contact" and "participate". The other table (attributes) contains more details about the "name" and also contains a "participate" value.

I would like to update table data with the "participate" value from table "attributes", if the "name" in table "attributes" matches either the "name" or "contact" in table "data".

I wrote this:

UPDATE data 
SET participate = (SELECT attributes.participate
FROM attributes
WHERE attributes.name = data.nam开发者_StackOverflowe)

This updates the "participate" value in the "data" table, but how can I also update the "participate" value if the name also appears in the contact field of the "data" table? If I run another query with:

WHERE attributes.name = data.contact

then this updates all values in the "data" table, rather than only the ones that match. Your help would be appreciated!


Give this a try:

update 
data as d inner join attributes as a on d.name = a.name or d.contact = a.name
set d.participate = a.participate


Try this:

update data d, attributes a
   set d.participate = a.participate
 where a.name = d.name or a.name = d.contact
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜