-2
I have the following code:
(SELECT id, slug
FROM anuncios
WHERE vum = '$idcategoria'
ORDER BY RAND())
ORDER BY publicado DESC
I need SQL to sort and at the same time Randomize, only that of the above code is only doing the sort, the randomization does not work.
Follow actual image of example:
The image above is what I’m trying to make work, note that we have two first advertisements have the field (advertising=2) with banner, and the last three without banner have the field (advertising=1). I need the first two that have the field (advertising=2) to be the first to be displayed (but at each page update these two advertisements randomize). Already the advertisements that have the field (advertising=1) remaining also randomize.
What do you mean? Either you order or you don’t order the data. The most you will get is to randomize a sample by placing a top 10 for example, so you would have 10 records chosen randomly but ordered by publication date.
– jean
@jean I need SQL to display ordered data because I have a field that has two values (published=1 and published=2) that sorts the published field in the ASC order, but that also Randomize.
– Gladison
Gladison, put a minimal example of how your records are and how you expect the return of select.
– Caique Romero
The clause
ORDER BY
will only sort by the fields you command, in relation to the other fields there is no ordering which means it will catch an "arbitrary order" (Random) in relation to the other fields but which, in general, will always catch the same arbitrary order.– jean
@Jean In this case how do I make Rand work too?
– Gladison
You don’t understand yet, the
RAND
is working and delivering a randomly ordered dataset for sorting by publication date but as this sorting ignores all other fields the other fields will come "cluttered" but will usually come cluttered in the same way– jean
Possible duplicate of How to select with ORDER BY and different criteria?
– Bacco
Just a published ORDER BY DESC, RAND() (see question from suggested closing link)
– Bacco