コラム2つのテーブルで条件を持っている行を削除

xikoseb:

私は、2つのテーブルを持っているtable_a、とtable_b私はからすべての行を削除する必要がtable_a私の作品以下、(および声明まで)で、:(ETIDがで、整数ですtable_a

delete m1 
from table_a m1
inner join table_a m2 on m1.etid = m2.etid and m1.id <> m2.id
where m1.gid = 198 and (here i need to make sure that each row with etid that fulfills above criteria and will be deleted, is also of type 'product', info which is stored in table_b)

table_b2つの列があります。 nidと、type

カラムの異なる値がtype2つである:'product'及び'raw'
nid列に格納されているINT番号であるetidtable_a

私は、別の参加を追加しなければならないと思うし、また声明では、ように...どのように上記のスニペットが変更されなければならないだけで、適切な行を選択しますか?私は、実行時間の速度の点で、また、これを行うには、最もパフォーマンスの高い方法を見つけるしたいと思います

https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=c16369b42847fbfb9a609e613945fc01

上記のサンプルデータ。

予想される出力

+----+------+---------+-----+
| id | etid |  title  | gid |
+----+------+---------+-----+
| 1  | 120  | Title1  | 198 | <-row that must be deleted
| 2  | 121  | Title2  | 55  |
| 3  | 120  | Title1  | 674 |
+----+------+---------+-----+

最終的な出力

+-----+------+---------+-----+
| id  | etid |  title  | gid |
+-----+------+---------+-----+
| 2   | 121  | Title2  | 55  |
| 3   | 120  | Title1  | 674 |
+-----+------+---------+-----+
離れて渡します:

あなたは別のに参加する必要がありますtable_b

delete m1 
from table_a m1 
inner join table_a m2 on m1.etid = m2.etid and m1.id <> m2.id
inner join table_b b on b.nid = m1.etid
where m1.gid = 198 and b.type = 'product'

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=6212&siteId=1