我有下表,该表用于自动应答,它跟踪交换给每个人的所有消息。它跟踪每个人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
)
现在,我有以下查询,该查询返回match_id
最后发送的消息是否own_reply
为false的行。(从上次系统回复他们时,就检查是否有人向系统发送了消息)
查询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
通过使用以下查询来确定程序对a进行了多少次答复
查询B
select count(*) from sms_log where match_id = ? and reply_batch = true
是否可以通过以下方式组合这两个查询,即只有当Reply_batch计数小于3时,QUERY A才返回match_ids?
您可以尝试以下方法:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句