mysql - Join only first matching record from joining table without duplicate -


i have 2 tables, 1. (inbox) keep delivery reports, 2. (outbox) keep send sms. can't add foreign key, , change datebase structure.

inbox

id    number       smsdate --    ----------   ------------------- 1     600600600    2013-08-16 11:51:18 2     700600600    2013-08-16 11:51:16 3     600600600    2013-08-16 11:51:14 4     900600600    2013-08-16 11:51:12 

outbox

id      number       processed_date ---     ----------   ------------------- 167     600600600    2013-08-16 10:51:10 288     700600600    2013-08-16 09:51:10 356     600600600    2013-08-16 08:51:10 473     900600600    2013-08-16 07:51:10 536     600600600    2013-08-16 06:51:10 

i join report of sent messages. can in such way comparing number , date of dispatch of table outbox, same number , nearest date of receipt of table inbox. sure reports in order.

if use

select outbox.id, inbox.id, outbox.number, inbox.number,     outbox.processed_date, inbox.smsdate outbox left join inbox on inbox.number= outbox.number     , inbox.smsdate >= outbox.processed_date group outbox.id order outbox.id desc; 

i'm getting strange results , reports duplicated. if have 3 sent, , 2 received, same number, should 1 empty. , instead of blank latter, duplicates previous one.

i tried add.

group outbox.id, inbox.id 

but worse.

it way solve this?

desired output:

output

outbox.id    inbox.id ---------    ----------      167         null     288          2     356          1    473          4     536          3 

my approach use correlated subquery inbox id, , join inbox table pull columns want:

select o.id, iid, o.number, i.number, o.processed_date, i.smsdate (select o.*,              (select i.id               inbox               i.number = o.number ,                     i.smsdate >= o.processed_date               order i.sms.date               limit 1              ) iid       outbox o      ) o left outer join      inbox      on o.iid = i.id order outbox.id desc; 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -