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  detail.orgid =4967219392022528) 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 ba.orgId = detail.orgId AND detail.inventoryId = ba.id

SET detail.propertyId = ba.propertyId

WHERE detail.`propertyId` IS NULL  AND  detail.orgid =4967219392022528;

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

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 voucher.orgId = detail.orgId

and voucher.id = detail.voucherId

where detail.orgId = #{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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值