大致是新增时一个字段的值是 子查询当前表然后 left join了另外一张表
table_a,table_b
insert into table_a(v1, v2, v3)
values ('v1', 'v2', if(exists(select id
from table_a a
left join table_b b on a.id = b.a_id
where a.v4 = 'v4'
and b.status in (1, 3)), 1, 2))
v3值是判断以前是否存在这个数据
代码写的是批量新增,测试数据三条就挂了
写法改了改,就好了
insert into table_a(v1, v2, v3)
values ('v1', 'v2', if(exists(select id
from table_a a
where a.v4 = 'v4'
and exists(select id from table_b b where a.id = b.a_id and b.status in (1, 3)), 1, 2))
有点没搞明白,百度也找不到答案,可能服务器也有点问题,尽量在外面查询出结果再insert吧