簡體   English   中英

如何從一個表中刪除少於?

[英]how to delete from one table where count less than?

我有2張桌子。

我使用此查詢來查找另一個表中一個表的字段出現次數

select t.id, t.tag_text, count(*) cnt from sms s, template as t
where s.id_template=t.id
group by  t.tag_text
order by  cnt desc

如果在短信中出現的行少於5次,如何從模板中刪除所有行? 這意味着如果count小於5.我使用MySQL。

您可以使用過濾器聚合結果,例如:

  select t.id, t.tag_text, count(*) cnt 
  from sms s, template as t
  where s.id_template=t.id
  group by  t.tag_text
  having count(*) <5 
  order by  cnt desc

並刪除例如:

  delete from  
  from sms as s
  inner join template as t on s.id_template=t.id
  group by  t.tag_text
  having count(*) <5 
delete from template 

where   id in
        (
            select      id_template
            from        sms
            group by    id_template
            having      count(*) < 5 
        )

暫無
暫無

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

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