简体   繁体   English

选择1个表,更新另一个表

[英]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.

相关问题 如何将 select 数据到另一个表并将其更新到其他表 - How to select data to another table and update it to other 从一张表中选择用户 ID 并在另一张表中更新 - Select User id from one table and update in another table MYSQL:基于来自另一个表的SELECT的更新行 - MYSQL: UPDATE rows based on SELECT from another table 再次选择后更新 Html 表 | 查询 | 阿贾克斯 - Update Html Table after another select | Jquery | Ajax 使用另一个数据库中的select query更新mysql表 - Update mysql table with select query from another database SELECT in UPDATE 在同一个表中 - SELECT in UPDATE in the same table 如何通过将一个表中的 id 与另一个表匹配来选择和更新一个表中的记录? - How to select and update records in one table by matching ids from it with another table? 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table 获取我从多个选择框中选择的所有ID并更新另一个表 - laravel - Get all IDs that I selected from multiple select box and update another table - laravel 尝试创建 Laravel 查询以选择,如果记录不存在则插入,如果存在于另一个表中则更新 - Trying to create laravel query to Select then Insert if record not exist, Update if exist in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM