我有這個疑問如何使用先前ID的結果進行SQL查詢?
SELECT thread_id, COUNT(message)
FROM xf_post
GROUP BY message
HAVING COUNT(message) > 1
和thread_id單的和計數的結果。
問題:如何使用此thread_id的查詢從其他表中刪除行?
我有這個疑問如何使用先前ID的結果進行SQL查詢?
SELECT thread_id, COUNT(message)
FROM xf_post
GROUP BY message
HAVING COUNT(message) > 1
和thread_id單的和計數的結果。
問題:如何使用此thread_id的查詢從其他表中刪除行?
wrape的內心另一個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
)
輝煌。 「temp」是我需要創建的表格嗎? – Argentina888 2015-04-05 16:17:00
nop。只需從要刪除的表名替換「yourtable」即可 – 2015-04-05 16:23:24
的通常辦法做到這一點是使用in
。如果要刪除具有多條消息的線程:
delete from othertable
where thread_id in (select thread_id
from xf_post
group by thread_id
having COUNT(message) > 1
);
不知道你在問什麼。請提供示例示例數據和完整的詳細信息。 – OldProgrammer 2015-04-05 16:03:12
你的問題不清楚。您正在通過'message'聚合,但選擇了'thread_id'。在大多數數據庫中,這會產生一個錯誤。請編輯你的問題並描述你想要完成的事情。 – 2015-04-05 16:42:02