时才能以查询a返回match_ids的方式组合这两个查询。
我有下表,该表用于自动响应器,它跟踪交换给每个人的所有消息。它通过match_id
CREATE TABLE public.sms_log
(
id bigint NOT NULL DEFAULT nextval('sms_log_id_seq'::regclass),
source text NOT NULL,
destination text NOT NULL,
message text,
insert_time timestamp with time zone DEFAULT now(),
reply_batch boolean DEFAULT false,
own_reply boolean DEFAULT false,
match_id text NOT NULL,
CONSTRAINT sms_log_pkey PRIMARY KEY (id),
CONSTRAINT sms_log_match_id_fkey FOREIGN KEY (match_id)
REFERENCES public.match (match_id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
现在,我有以下查询,如果最后发送的消息将own_reply
作为false,则返回match_id
的行。(几乎可以检查某人是否从系统回复给它们的最后一次消息发送了系统)
查询A
select m.*
from sms_log m
where m.source <> 'MYNUMBER'
and m.destination = 'MYNUMBER'
and m.insert_time = (select max(insert_time)
from sms_log
where match_id = m.match_id
group by match_id)
然后,我使用程序内使用一个循环来确定程序已通过以下查询
对match_id
的回复
查询b
select count(*) from sms_log where match_id = ? and reply_batch = true
是否可以以查询A仅在回复_batch计数小于3?
您可以尝试以下方法:
select m.*
from sms_log m
where m.source <> 'MYNUMBER'
and m.destination = 'MYNUMBER'
and m.insert_time =
(select max(insert_time)
from sms_log
where match_id = m.match_id group by match_id)
and (select count(*)
from sms_log as sl
where sl.match_id = m.match_id
and sl.reply_batch = true) < 3