简体   繁体   中英

How can I make an UPDATE query on INNER JOIN using OR

I want to update the values of one column in a table from '0' to '1', if either of four values in columns in another table are '1'. Somehow this doesn't seem to work and I was just wondering if anyone could help me get the code right or find a different way of doing it if it's not possible,

mysql_query("UPDATE members 
            INNER JOIN forum_banners ON members.id = forum_banners.userid
            SET members.beta = '1' WHERE forum_banners.bebeta = '1' OR 
            forum_banners.bibeta = '1' OR forum_banners.cbeta = '1' OR 
            forum_banners.wbeta = '1'") or die(mysql_error()); 

That's what I tried, but it's not working, I suspect because of the OR. I tried having all updatings in different mysql_query bits, but that didn't work either.

You should be able to update from multiple table references. This is untested, but gives you an idea:

UPDATE 
    members, forum_banners 
SET 
    members.beta = '1'
WHERE 
    members.id = forum_banners.userid 
    AND forum_banners.bebeta = '1' 
    OR forum_banners.bibeta = '1' 
    OR forum_banners.cbeta = '1' 
    OR forum_banners.wbeta = '1'

http://dev.mysql.com/doc/refman/5.0/en/update.html

Note "Multi-Table syntax"

Try

UPDATE
    m
SET 
    m.beta = '1'
FROM 
    members m
INNER JOIN 
    forum_banners fb
    ON m.id = fb.userid
WHERE 
    fb.bebeta = '1' 
    OR fb.bibeta = '1' 
    OR fb.cbeta = '1' 
    OR fb.wbeta = '1'"      

Aliases also help make your syntax a little neater.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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