mysql - Join Two Tables and Replace Values Where Conditions Meet -


i'm trying come expression join 2 tables (the first two) third one.

i want select 'sitepage' , 'medium' , join first 2 tables on rows right 5 characters matching between 'sitepage' , 'campaign id'. additionally, if there match, 'program' replace 'medium'. syntax?

sitepage | medium                  xyz.com/campaign=12345 |            xyz.com/campaign=23456 | c          campaign id | program                  12345 | b             sitepage | medium                  xyz.com/campaign=12345 | b            xyz.com/campaign=23456 | c         

http://i.stack.imgur.com/pq35n.png

i based answer off of @juan's, had make adjustments work.

select   sitepage, coalesce(t2.program, t1.medium) medium table1 t1 left join table2 t2 on right(t1.sitepage, 5) = coalesce(t2.`campaign id`, -1); 

@abhik heading in right direction too. it's more generic 1 above, assumes last 5 characters of sitepage pertinent ones. said, have gone with...

select   sitepage, coalesce(t2.program, t1.medium) medium table1 t1 left join table2 t2   on substring_index(t1.sitepage,'=',-1)     = coalesce(t2.`campaign id`, -1); 

sql fiddle example


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -