2016-11-08 23 views
1

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

場的出現次數的數量如何從模板中刪除所有行,如果他們在短信occure小於5倍的例子嗎?這意味着如果count小於5.我使用MySQL。

+0

見http://meta.stackoverflow.com/questions/333952/爲什麼-應該-I-提供-AN-MCVE換什麼,似乎對我將要-A-極簡單的SQL查詢 – Strawberry

回答

0
delete from template 

where id in 
     (
      select  id_template 
      from  sms 
      group by id_template 
      having  count(*) < 5 
     ) 
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 
相關問題