SELECT r.name FROM tb_offer_role r LEFT JOIN ( SELECT d.offer_nc, d.id, d.da_id, IF(d.da_id = #{daId}, 1, 0) sort_no FROM tb_offer_detail d WHERE d.offer_id = #{offerId} ) a ON r.name = a.offer_nc WHERE r.can_enable = 1 AND (a.id IS NULL OR a.da_id = #{daId}) ORDER BY a.sort_no DESC, RAND() LIMIT 1 INSERT INTO tb_offer_detail(id, offer_id, price, is_kxc, offer_time, offer_name, offer_phone, offer_nc, offer_wxnc, deposit, da_id, is_push, push_content, is_slpush, push_slcontent, order_no, source) VALUES(#{id}, #{offerId}, #{price}, #{isKxc}, #{offerTime}, #{offerName}, #{offerPhone}, #{offerNc}, #{offerWxnc}, #{deposit}, #{daId}, #{isPush}, #{pushContent}, #{isSlPush}, #{pushContent}, #{orderNo}, #{source})