/* Experiment object: two student tables 1. One stu student table, one stu1 student table. 2. The above table has three fields (student id, student gender, student name)*/
/* The common scenarios of the update statement are divided into two categories: 1. Single table update 2. Multi-table association update*/
-- 1.1 Single form update form field
update stu t set t.NAME = 'mike' where t.ID = '1';
-- 1.2 Single table update with multiple fields
update stu t set t.NAME = 'mike', t.SEX = '1' where t.ID = '2';
/* When updating multiple tables, remember to add the exists() condition, otherwise the records that do not meet the conditions will be called NULL by the update: For example, if the stu table exists, but the data in the stu1 table does not exist, the corresponding field will be updated to NULL ;*/
-- 2.1 Multi-table association update single field
update stu t set t.NAME = (select t1.NAME from stu1 t1 where t1.ID = t.ID)where exists(select 1 from stu1 t2 where t2.ID = t.ID);
-- 2.2 Multi-table association update multi-field
update stu t set (t.NAME, t.SEX) = (select t1.NAME, t1.SEX from stu1 t1 where t1.ID = t.ID)where exists(select 1 from stu1 t2 where t2.ID = t.ID);
Four common ways of writing SQL Update
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325989899&siteId=291194637
Ranking