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
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
Post a Comment