ORDER BY RAND() in 2 columns

Asked

Viewed 158 times

0

inserir a descrição da imagem aqui

Well earlier I posted a problem about the elimination of 1 select which was solved but now I appeared another problem .

My idea is to create a playlist that prioritizes status 1. Whenever a user posts the video it is inserted with status 1. After the video is played, I update it to status 0. The playlist will continue playing videos that have status 0

The problem is that when all videos are status=0 the playlist will always be catching the same video in the case of select below will always get id 7, I’m trying to randomize unsuccessfully.

SELECT * FROM playlist ORDER BY status DESC,id ASC, RAND() LIMIT 1
  • SELECT * FROM playlist ORDER BY status DESC,RAND() LIMIT 1

1 answer

2

When using id ASC, RAND() means that he will order first by id and if it has two lines with the same value it will use the RAND() to sort. Only that will never happen because there will never be two primary keys (id) equal in the same table. To fix this remove the id ASC of the Code

In addition, the use of the primary key ascendantly as the second sorting option is downgrading, the table data are already naturally sorted by id, when making any consultation, with or without ORDER BY, the line of id 1 will never be in front of the line of id 0, unless by order of another field (if any)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.