この問題を解決するのを手伝っていただけませんか。3つのテーブルがあります。
Table Name1: Contact
cnt_id, CNTName
12, test
13, test2
Table Name2: Relation
cnt_id, r_grpid
12, 55
13, 55
13, 56
Table Name3: Group
grp_1d,
55
56
私が探しているのは、テーブルからグループID 55を削除すると、それに応じてテーブルGROUP
のレコードがCONTACT
削除されることです。ただし、ご覧のとおりCNT_id 13
、この場合は別のグループにもリンクCNT_id 13
されています。削除しないでください。
よろしく
Relation内のcnt_idとr_grpidの組み合わせが一意である場合は、前に提案したクエリを使用できますが、cnt_idに次のような追加のフィルターを使用します。
declare @groupId int =55
delete from Contact where cnt_id in (select C.cnt_id from Contact C
inner join Relation R on R.cnt_id=C.cnt_id where R.r_grpid =@groupId AND cnt_id not in (select cnt_id from Relation group by cnt_id having count(*) > 1))
delete from Relation where r_grpid =@groupId AND cnt_id not in (select cnt_id from Relation group by cnt_id having count(*) > 1)
delete from Group where r_grpid =@groupId
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加