如何使用以前的ID结果进行SQL查询?

时间:2015-04-05 15:53:53

标签: sql

我有这个查询

SELECT thread_id, COUNT(message) 
FROM xf_post 
GROUP BY message 
HAVING COUNT(message) > 1 

和thread_id和count的结果。

问题:如何使用此thread_id进行查询以从其他表中删除行?

2 个答案:

答案 0 :(得分:0)

在另一个select语句中清除查询,只选择thread_id

Delete from yourtable where id in 
(
SELECT temp.thread_Id 
From    
 (  SELECT thread_id, COUNT(message) 
          FROM xf_post 
           GROUP BY message 
           HAVING  COUNT(message) > 1 
 ) temp      
)

你也可以这样做

 with cte as
 (
          SELECT thread_id
          FROM xf_post 
           GROUP BY message 
           HAVING  COUNT(message) > 1   
  )

 Delete from yourtable where id in 
  (
 select  thread_id from cte
  )

答案 1 :(得分:0)

通常的方法是使用in。如果要删除包含多条消息的线程:

delete from othertable
    where thread_id in (select thread_id
                        from xf_post 
                        group by thread_id
                        having COUNT(message) > 1 
                       );