MySQL: Join on same table with sorting -


i performing join on same table t(type_id,place_name) on different type_id :

------------------ type_id|place_name -------|----------   1    |      1    |    b   2    |    x   2    |    y ------------------ 

my query :

select t1.type_id, t1.place_name t t1 join t t2 on       t1.type_id!= t2.type_id ,     t1.place_name!=t2.place_name; 

i result:

------------------- type_id|place_name -------|-----------    2   |    x    2   |    y    2   |    x    2   |    y    1   |       1   |    b    1   |       1   |    b  ------------------- 

but want result like:

------------------- type_id|place_name -------|-----------    1   |       2   |    x    1   |       2   |    y    1   |    b    2   |    x    1   |    b    2   |    y  ------------------- 

i need sort result using order by. please help.

if want kind of zipped order, this:

select t1.type_id, t1.place_name t t1 join t t2 on       t1.type_id!= t2.type_id ,     t1.place_name!=t2.place_name order   least(t1.place_name, t2.place_name),   greatest(t1.place_name, t2.place_name),   t1.type_id 

sqlfiddle

since can't explain logic of order, can't tell if work other data. instead of place_name might need use combination of both columns.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -