2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > mysql查询更新删除_mysql查询 更新(text类型) 删除关联操作

mysql查询更新删除_mysql查询 更新(text类型) 删除关联操作

时间:2020-07-12 10:49:10

相关推荐

mysql查询更新删除_mysql查询 更新(text类型) 删除关联操作

注意:mysql中不支持全关联,支持左外,右外关联,oracle支持全关联查询

1.今天做了这么一个需求,简单抽离说明一下:操作表:biz_pu_arrival_detail 关联表:ba_inventory

需求是将 关联表中的 propertyId 赋值给 操作表中的propertyId,关联条件是操作表中的 inventoryId字段和关联表中的id 关联。

错误解答:开始写的sql是这样的:

UPDATE (SELECT detail.id,detail.propertyId,ba.propertyId AS pid FROM biz_pu_arrival_detail detail JOIN ba_inventory ba ON detail.inventoryId = ba.id WHERE detail.`propertyId` IS NULL AND id =496721939528) a SET a.propertyId = a.pid ;

因为 update table t set t.oldName = t.newName 是可以执行的;

但是执行结果报The target table a of the UPDATE is not updatable,原因是不能对虚拟视图做操作,update数据需要对物理表操作;

正确解答:

UPDATE biz_pu_arrival_detail detail

JOIN ba_inventory ba ON Id = Id AND detail.inventoryId = ba.id

SET detail.propertyId = ba.propertyId

WHERE detail.`propertyId` IS NULL AND id =496721939528;

知识点:关联语句不仅仅只有关联查询,还可以关联更新,同样可以关联删除操作,之前只知道可以关联查询,特此记录

2.具体关联形式

2.1 双等号关联

select a.*,b.* from a,b where a.pid = b.pid

2.2 外关联(左外关联,右外关联,全外关联)

select a.*,b.* from a

left(right,full)(outer )join b on a.pid = b.pid

where a.name is not null ...

左外和右外关联 只是主表不同,在 a,b表中有对应不上的数据时,如果左外关联 那么会把a 中的所有数据查询,b表中没有对应的关联数据时,为null,右外关联相反,全外关联 是左外和右外的和

2.3 内关联

select a.* ,b.* from a join b on a.pid =b.pid

或者 select a.*, b.* from a inner join b on a.pid = b.pid;

3关联删除操作:

delete detail, voucher

from biz_st_rdrecord_detail detail

inner join biz_st_rdrecord voucher on Id = Id

and voucher.id = detail.voucherId

where Id = #{orgId}

and voucher.sourceVoucherTypeId in (1000030008, 1000030009);

加个塞!!!!

之前做了一个需要更新大字段text类型的字段内容,内容用于关键字匹配用,现在需要增加关键字了,如何更新,这里因为数据库不止一个,需要写升级脚本,同时执行

正常字段的update:update table set column1 = '' where id = 123;

道理一样text更新内容:update table set keyStr = concat(keyStr,'abc',' def') where id = 123;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。