簡體   English   中英

從表 B 中刪除,其中列值類似於帶有通配符的表 a

[英]Delete from table B where column value like table a with wildcards

所以我有兩張桌子

seeds
- id
- domain

subdomain
- id 
- domain
- ip

我想根據域過濾子域

例如

seeds
Id  Domain
0   google.com
1   test.com

subdomain
Id   domain          ip
0    test.google.com    null
1    api.google.com     null
2    dnr.com            null
3    neverssl.com       null

我正在嘗試編寫一個查詢來刪除subdomain不包含來自seedsdomain的行

你嘗試過什么?

delete subdomain 
where id not in 
(select subs.id from seed as seeds 
join 
subdomain as subs on subs.domain 
like concat('%', seeds.domain));

delete subdomain 
where id not in
(SELECT sd.id
FROM subdomain sd
LEFT JOIN seed s
  ON sd.domain LIKE CONCAT('%', s.Domain)
WHERE s.id IS NULL)

這兩個查詢都只是刪除所有行

您可以使用not exists

delete from subdomain
where not exists (
    select 1
    from seeds s
    where subdomain.domain like concat('%', s.domain)
)

演示

使用LEFT JOIN + NULL模式查找不匹配的行。

DELETE d
FROM subdomain AS d
LEFT JOIN seeds AS s ON d.domain LIKE CONCAT('%', s.domain)
WHERE s.id IS NULL

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM