update 语句用于在数据库表中修改数据
语法:
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value;
例如:
UPDATE test1
SET firist_name = lilsa
WHERE user.city = london;
多表链接修改:
UPDATE test1
INNER JOIN test2
ON test1.id = test2.user_id
SET test1.is_admin=test2.is_admin
WHERE test1.id<100;
等同于
UPDATE test1,test2
SET test1.is_admin=test2.is_admin
WHERE test1.id = test2.user_id AND test1.id<100;
将一个表中某个字段的值赋给另一个表的字段:
UPDATE test1
SET score = score + 5
WHERE test1.id in (SELECT test1_id from test2 where s_id = 7);
UPDATE 也可以用left join、inner join来进行关联,执行效率会更高
UPDATE test1 t1 inner join test12 t2 on t1.id = t2.test1_id
SET score = score + 5
WHERE t2.s_id = 7;