php - mysql linked list sort query, why is it working? -
my table looks this:
+------------------------+ | id | title | position | +------------------------+ | 1 | test 2 | 3 | +------------------------+ | 2 | test 3 | 1 | +------------------------+ | 3 | test 1 | 0 | +------------------------+ i found query retrieves rows ordered based on position field holds id of predecessor.
select * mytable t1 left bring together mytable t2 on t2.position = t1.id i wonder why working because there no order clause , database should't know position 0 row start at.
the result dependent on order inserted rows table. if, example, had inserted row id=3 before inserted row id=2, have got non-sorted result.
as stands, pulling info out of t1 in order of id because order set elements table
see http://sqlfiddle.com/#!2/63a925/2 , seek yourself.
n.b. databases not guaranteed work state, databases work way. should not rely on behaviour minor alter schema or query ruin whole day! note if id (primary?) key, insert order overridden fact database pull rows out in order of index.
php mysql sql linked-list
No comments:
Post a Comment