sql - Matching rows across tables -


i looking way match rows b/w 2 tables.

name_tokens ----------------- product_id, tokens ---------------- 1, token 1 1, token 2 1, token 3 2, token 1 2, token 2 3, token 1 4, token 1  ----------- models_tokens --------------- product_id, tokens ------------------ 1, token 1 1, token 2 2, token 1 3, token 1 4, token 1 

so 1 table contains model tokens of product , other 1 contains name tokens of product. want products name tokens contains model tokens.

it go this. 1.) each product see how model token there product 2.) check name tokens existence of each model token of product. if yes counts match.

i hope made scenerio clear. if not requested question.

edit

--------- name_tokens ----------------- product_id, tokens ---------------- 1, hello 1, world 1, stackoverflow 2, stack  2, overflow 3, stack 4, flow  ----------- models_tokens --------------- product_id, tokens ------------------ 1, hello 1, stackoverflow 2, ovreflow 3, overflow 4, stack 

so result need is

product_id 1 2 

you left outer join , aggregation:

select nt.product_id name_tokens nt left outer join      model_tokens mt      on nt.product_id = mt.product_id ,         nt.tokens = mt.tokens group nt.product_id having count(mt.product_id) = count(*); 

what query start name_tokens table , find matches in model_tokens table. if there no match, null assigned rows in mt (courtesy of left outer join). having clause returns products have equal number of matches in second table there tokens in first.

edit:

for reverse, can reverse query:

select mt.product_id model_tokens mt  left outer join      name_tokens nt      on nt.product_id = mt.product_id ,         nt.tokens = mt.tokens group mt.product_id having count(nt.product_id) = count(*); 

note having clause changes order of joins , group by clause.


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 -