如何从一个表中删除少于?

时间:2016-11-08 13:34:14

标签: mysql sql

我有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。

2 个答案:

答案 0 :(得分:0)

您可以使用过滤器聚合结果,例如:

  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 

答案 1 :(得分:0)

delete from template 

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