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;