56,679
社区成员
发帖
与我相关
我的任务
分享
update user_address as t
set t.IS_DEFAULT = 1
where t.USER_ID = 'ifelse'
and not exists (
select 1 from user_address i
where i.USER_ID = t.USER_ID and ifnull(i.IS_DEFAULT, 0) = 1
) and t.ADDRESS_ID in (
select min(i.ADDRESS_ID)
from user_address i
where i.USER_ID = t.USER_ID
)
update user_address t inner join (
select t.USER_ID, ifnull(sum(t.IS_DEFAULT), 0) isDefault
from user_address t where t.USER_ID = 'ifelse'
group by t.USER_ID
) i on i.USER_ID = t.USER_ID
inner join (
select t.USER_ID, min(t.ADDRESS_ID) minAddressId
from user_address t where t.USER_ID = 'ifelse'
group by t.USER_ID
) j on j.USER_ID = t.USER_ID
set t.IS_DEFAULT = 1
where i.isDefault <= 0 and j.minAddressId = t.ADDRESS_ID