[英]Select 1 table, update another
I'm trying to update users within a table, that have rank 3 in another table. 我正在尝试更新表中的用户,在另一个表中排名为3。 Here's an example:
这是一个例子:
t1: T1:
id respect activity_points
1 10 1200
2 10 700
3 10 90
t2: 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. 我正在尝试更新table1中的rank',其中rank等于2或更高,来自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 我认为你正在寻找SQL答案而不是PHP,并假设t2.id是映射到t1中id的id
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.