Wednesday, April 11, 2012

MySQL select procedure

I have little weird issue here. So, I have these two tables. For example:



tbl1            tbl2

col1 col1 col2
---- ---- ----
BET BET 0
EMK EMK 1
BET EMK 3
EMK BET 4
BET BET 5
ANC


What I would like to do is to have a procedure (for now a select query will do) that selects data from tbl1 and compares it to tbl2 and return the following result:



col1  col2
---- ----
BET 0
EMK 1
BET null
EMK 3
BET 4
ANC 5


I have no idea how to even begin this... Is there any way to, maybe use an if..else within a select statement?... Please, help! Thank you in advance!



EDIT



I'm sorry for not being very clear.
I need it to to show null for an item in the first table that does not follow the order of the second table.
Here we have BET in both tbl1 and tbl2, therefore col2 should be 0,
next, EMK in both -> col2 - 1
next, BET is not in tbl2 (it does not go right after row 2 with value EMK) - set col2 to null.
next, EMK in tbl1, there is also EMK in tbl2, that goes after previous EMK, set col2 to 3
next, BET in tbl1 and tbl2, set col2 - 4
next, ANC in both tables, col2 - 5.



I apologize for this gibberish, but this the best I can do to explain this logic...





No comments:

Post a Comment