// Update with conditions u.WithContext(ctx).Where(u.Activate.Is(true)).Update(u.Name, "hello") // UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE active=true;
// Update with conditions u.WithContext(ctx).Where(u.Activate.Is(true)).Update(u.Age, u.Age.Add(1)) // or u.WithContext(ctx).Where(u.Activate.Is(true)).UpdateSimple(u.Age.Add(1)) // UPDATE users SET age=age+1, updated_at='2013-11-17 21:34:10' WHERE active=true;
u.WithContext(ctx).Where(u.Activate.Is(true)).UpdateSimple(u.Age.Zero()) // UPDATE users SET age=0, updated_at='2013-11-17 21:34:10' WHERE active=true;
// Select with Map // User's ID is `111`: u.WithContext(ctx).Select(u.Name).Where(u.ID.Eq(111)).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false}) // UPDATE users SET name='hello' WHERE id=111;
u.WithContext(ctx).Omit(u.Name).Where(u.ID.Eq(111)).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false}) // UPDATE users SET age=18, active=false, updated_at='2013-11-17 21:34:10' WHERE id=111;
result.RowsAffected // affect rows number err // error
根据子查询进行更新
使用子查询更新单个字段
u := query.User c := query.Company
u.WithContext(ctx).Update(u.CompanyName, c.Select(c.Name).Where(c.ID.EqCol(u.CompanyID))) // UPDATE "users" SET "company_name" = (SELECT name FROM companies WHERE companies.id = users.company_id);
ua.WithContext(ctx).UpdateFrom(ca.WithContext(ctx).Select(c.ID, c.Address, c.Phone).Where(c.ID.Gt(100))). Where(ua.CompanyID.EqCol(ca.ID)). UpdateSimple( ua.Address.SetCol(ca.Address), ua.Phone.SetCol(ca.Phone), ) // UPDATE `users` AS `u`,( // SELECT `company`.`id`,`company`.`address`,`company`.`phone` // FROM `company` WHERE `company`.`id` > 100 AND `company`.`deleted_at` IS NULL // ) AS `c` // SET `u`.`address`=`c`.`address`,`c`.`phone`=`c`.`phone`,`updated_at`='2021-11-11 11:11:11.111' // WHERE `u`.`company_id` = `c`.`id`