0
If the title of my question was confused, I explain: I want to sort a result of a certain query of MYSQL
.
Example:
SELECT * FROM
usuarios
ORDER BY
cidade = 'BH' DESC,
cargo = 'Programador' DESC
However, I would like that, if more than one user fell in the same question of ordering (for example, the user has the same city and position in this ordination), the ordination was random.
Example:
| nome | cargo | cidade |
-----------------------------------------
| wallace | programador | bh |
| helbert | programador | bh |
| simão | programador | sp |
| miguel | venda | sp |
In the above example, Wallace and helbert marry the ordination of the cargo
and cidade
simultaneously.
However, the criterion that the MYSQL
will use after this ordering, wanted between these two (when they are identical) the result is random.
That is, if the results of ORDER BY
result in the same amount of items that match the ordering, I want the next ordering between them to be random (one hour Wallace will be first, another time may be helbert).
How do I do that in the MYSQL
?
Qnd vc behind the database the fetch array in variavek is not so $query[0]['name']. You could make use of the hand with the limit of queries example has 10 queries vc makes a $aleatorio= hand(0.9). And when I would use the array I would put it in the $query [$randomly]['name']. And if you do not know the limit of each query has a function that tells the size of the array or uses Count I do not remember well.
– Thalles Daniel
@Thallesdaniel did not understand anything. I use the
Laravel
for this, I do not use thePDO
pure or themysqli_
. But the question is not related to PHP but withMYSQL
.– Wallace Maxters
Your query in the database returns a correct array?
– Thalles Daniel
Yes. You’re not suggesting I use a
shuffle
in thearray
, right? Because it would invalidate my ordering from the bank. As I said, I want the solution ONLY inMYSQL
.– Wallace Maxters
you do not that when office and city is equal do a hand in the correct name?
– Thalles Daniel
I believe it is easier to bring the consultation and then make a hand with the consultation in the position condition and city are equal.
– Thalles Daniel
Wallace, I don’t know the possibility of this intelligence in Mysql. We need to analyze all the points. Why do you need this? How do you want the data output? Why can’t the data output be as is mysql output? I see no use for the data to come out like this. Also remember the responsibility of data, sometimes not everything is in charge of DBA and yes of dev.
– juniorb2ss
You can do yes, I found out. I’m only analyzing now which of the ways will be the best to post here
– Wallace Maxters