我有以下两个表
CREATE TABLE message_log
(
id integer,
message text,
from_id character varying(500),
to_id character varying(500),
match_id character varying(500),
own_account boolean,
reply_batch boolean DEFAULT false,
insert_time timestamp with time zone DEFAULT now()
)
CREATE TABLE likes
(
id integer,
userid character varying(500),
insert_time timestamp with time zone DEFAULT now()
)
如果没有发送包含“@”的相同match_id
的邮件,我有以下查询返回match_ids。
select distinct(match_id) from message_log where own_account = TRUE and match_id not in
(select match_id from message_log where message like '%@%')
我还要返回to_ids,因为在我想构建的查询中需要它们,所以我将查询修改为
select distinct(match_id, to_id) from message_log where own_account = TRUE and match_id not in
(select match_id from message_log where message like '%@%')
现在我想创建一个查询,如果从上面的查询返回的to_id
与likes表中的userid
匹配,则会删除likes表中的任何行。是否可以在一个查询中执行此操作?
答案 0 :(得分:1)
这样的事情应该有效:
delete from b
from (
select distinct match_id, to_id
from message_log
where own_account = TRUE and match_id not in (select match_id from message_log where message like '%@%')
) a inner join likes b on a.to_id = b.userid
基本上,只需将你的结果和内部联接放在你喜欢的表上,以确定从喜欢的表中删除哪些结果。