简体   繁体   中英

Select 1 table, update another

I'm trying to update users within a table, that have rank 3 in another table. Here's an example:

t1:

id   respect   activity_points
1    10        1200
2    10        700
3    10        90

t2:

id   rank
1    3
2    1
3    1

I'm trying to update 'respect' in table1 where rank is equal to 2 or above, from table2.

Thanks in advance :)

I think you are looking for the SQL answer not PHP and assuming that t2.id is id that maps to the id in t1

update t1 
inner join t2 on t1.id = t2.id
set t1.respect = (t1.respect + 5) 
where t2.rank >= 2
Query : update t1 set respect=newvalue where id 
        in ( select id from t2 where rank > 2 )
UPDATE t1 SET respect = 'your value' WHERE id in (
    SELECT id FROM t2 WHERE rank >= 2
)

or

UPDATE t1 SET respect = 'your value' WHERE id = (
    SELECT id FROM t2 WHERE rank >= 2
)

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