mysql - order by: if two or more result match with limit 1 then randomize -


select * my_database order priority desc limit 1 

my_database:

 name | priority  ##################  dave |   100  pat  |   100  jo   |    99 

1: what's deciding factor when there's 2 possible results , limit set 1?

2: can add randomization between 2 possible matches? how?

1: what's deciding factor when there's 2 possible results , limit set 1?

unless specify ordering primary key (or combination of fields make clear db how order), cannot predict of 2 possible results display. database engine determine that.

i tried query (example: http://www.sqlfiddle.com/#!9/e2df77/2) select * test order priority desc limit 1 , pat showed up. tried select * test priority = 100 order priority desc limit 1 , dave showed up. moral: don't count on results unless tell database unambiguously how order information.

2: can add randomization between 2 possible matches? how?

you can order query using rand().

select * test priority = 100  order priority desc, rand() desc limit 1 

a variant of above can used randomization. order rand() or order field, rand() work.

example: http://www.sqlfiddle.com/#!9/e2df77/3


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -