Programmation » [MySQL] ORDER by RAND()
Catégorie:  
   
[MySQL] ORDER by RAND()
Publié le 07/12/2007 @ 17:56:56,
Par cauet
Hello,

SELECT nom FROM table ORDER by RAND() LIMIT 1;

Le Random de MySQL me parait bien bizzare..
Exemple d'une sortie:


PUB 00h07 -> M2
PUB 00h20 -> CUTTINGCREW2
PUB 00h40 -> SHAKATAC2
PUB 00h50 -> HOOBASTANK2
PUB 01h07 -> HOOBASTANK2
PUB 01h20 -> CUTTINGCREW2
PUB 01h40 -> C
PUB 01h50 -> YELLOVICIOUSGAMES2
PUB 02h07 -> EAMON2
PUB 02h20 -> RAPHAEL2
PUB 02h40 -> HERBALPERT2
PUB 02h50 -> HERBALPERT2
PUB 03h07 -> HERBALPERT2
PUB 03h20 -> ARTOFNOISE2
PUB 03h40 -> A
PUB 03h50 -> CALI2
PUB 04h07 -> NATE JAMES2
PUB 04h20 -> ALICIAKEYS2
PUB 04h40 -> CORNEILLE2
PUB 04h50 -> A
PUB 05h07 -> CALI2
PUB 05h20 -> SHAKATAC2
PUB 05h40 -> CARLABRUNI2
PUB 05h50 -> ARTOFNOISE2
PUB 06h07 -> KEANE2
PUB 06h20 -> M2
PUB 06h40 -> RAPHAEL2
PUB 06h50 -> SHAKATAC2
PUB 07h07 -> MARCLAVOINE2
PUB 07h20 -> ALICIAKEYS2
PUB 07h40 -> ENYAORINOCO2
PUB 07h50 -> ENYAORINOCO2
PUB 08h07 -> SCISSOR2
PUB 08h20 -> CRAIGDAVID2
PUB 08h40 -> CRAIGDAVID2
PUB 08h50 -> ELTONJOHN2
PUB 09h07 -> M2
PUB 09h20 -> ENYAORINOCO2
PUB 09h40 -> GORILLAZ2
PUB 09h50 -> CUNNIEWILLIAMS2
PUB 10h07 -> PLACEBO2
PUB 10h20 -> GNARLS
PUB 10h40 -> GORILLAZ2
PUB 10h50 -> C
PUB 11h07 -> PLACEBO2
PUB 11h20 -> NATE JAMES2
PUB 11h40 -> ANGIESTONE
PUB 11h50 -> SCISSOR2
PUB 12h07 -> KEANE2
PUB 12h20 -> NATE JAMES2
PUB 12h40 -> CUNNIEWILLIAMS2
PUB 12h50 -> SCISSOR2
PUB 13h07 -> CRAIGDAVID2
PUB 13h20 -> ELTONJOHN2
PUB 13h40 -> SCISSOR2
PUB 13h50 -> NATE JAMES2
PUB 14h07 -> MARCLAVOINE2
PUB 14h20 -> CORNEILLE2
PUB 14h40 -> PLACEBO2
PUB 14h50 -> CALI2
PUB 15h07 -> CALI2
PUB 15h20 -> MARCLAVOINE2
PUB 15h40 -> CUNNIEWILLIAMS2
PUB 15h50 -> PLACEBO2
PUB 16h07 -> ENYAORINOCO2
PUB 16h20 -> A
PUB 16h40 -> MASSIVEATTACK2
PUB 16h50 -> GORILLAZ2
PUB 17h07 -> NATE JAMES2
PUB 17h20 -> ARTOFNOISE2
PUB 17h40 -> CHADKROGER2
PUB 17h50 -> CUNNIEWILLIAMS2
PUB 18h07 -> RAPHAEL2
PUB 18h20 -> CORNEILLE2
PUB 18h40 -> MARCLAVOINE2
PUB 18h50 -> ENYAORINOCO2
PUB 19h07 -> CORNEILLE2
PUB 19h20 -> ALICIAKEYS2
PUB 19h40 -> SHAKATAC2
PUB 19h50 -> GNARLS
PUB 20h07 -> DELASOUL2
PUB 20h20 -> M2
PUB 20h40 -> YELLOVICIOUSGAMES2
PUB 20h50 -> EVANESCENCE2
PUB 21h07 -> GNARLS
PUB 21h20 -> CRAIGDAVID2
PUB 21h40 -> ANGIESTONE
PUB 21h50 -> ARTOFNOISE2
PUB 22h07 -> KEANE2
PUB 22h20 -> CARLABRUNI2
PUB 22h40 -> ALICIAKEYS2
PUB 22h50 -> CALI2
PUB 23h07 -> HERBALPERT2
PUB 23h20 -> DSTEWART2
PUB 23h40 -> HERBALPERT2
PUB 23h50 -> HERBALPERT2


Il me sort assez souvent les mêmes résultats à répétition..
Y'a pas moyen de "casser" ça?

Merci
   
[MySQL] ORDER by RAND()
Publié le 07/12/2007 @ 20:07:09,
Par chou_
en fesant un GROUP BY non?
   
[MySQL] ORDER by RAND()
Publié le 07/12/2007 @ 22:49:39,
Par ovh
Le group by ne sert qu'à regrouper des résultats qui apparaîtraient plusieurs fois (plusieurs lignes) sur base d'une colonne. Ici, la requête ne renvoit chaque fois qu'une seule ligne, donc c'est inutile.
Je n'ai rien à voir avec www.ovh.com
   
[MySQL] ORDER by RAND()
Publié le 07/12/2007 @ 22:58:40,
Par max
Essaye une autre approche:

- tu connais ou tu récupère le nombre d'enregistrement de ta table.
- tu génères un nombre aléatoire compris en 0 et le nombre d'enregistrement -1.

et ensuite select name from table limit $x,1; $x étant le nombre généré plus haut.
Trololo
   
[MySQL] ORDER by RAND()
Publié le 07/12/2007 @ 23:12:41,
Par cauet
Pas faux max.. mais je vais tomber aussi dans le travers de Random() sous Linux dont le Rand() de MySQL est grandement inspiré je penses.. non? :write:
   
[MySQL] ORDER by RAND()
Publié le 08/12/2007 @ 16:26:50,
Par ovh
Si tu génères ton random dans un autre langage (PHP par exemple :grin: ), tu as le loisir d'utiliser d'autres fonctions plus évoluées. Le random() de base est assez élémentaire, mais il y a moyen de peaufiner au moyen de la technique du grain de sable :
http://nicolasj.developpez.com/articles/libc/hasard/ (exemple en C mais le principe reste le même et je pense qu'il y a des fonctions équivalentes en PHP)
http://www.php-scripts.com/php_diary/122799.php3
Je n'ai rien à voir avec www.ovh.com
Répondre - Catégorie:  
Informaticien.be - © 2002-2024 AkretioSPRL  - Generated via Kelare
The Akretio Network: Akretio - Freedelity - KelCommerce - Votre publicité sur informaticien.be ?