8
Using as an example, I have a certain user table, where that user’s city is, is a reference to another table.
I need to order these users in a query according to the location of the current logged in user.
Example, if I live in Belo Horizonte
, the user data of the Belo Horizonte
.
I know that in Mysql the ORDER BY
acts as a means of sorting by the value in the column, but I don’t know how to sort by specific values.
For example, I want the order to be: Belo Horizonte
and from then on it makes no difference.
In a fictional example I’ll show you what I want:
First I take the city’s value:
$valor = Auth::user()->cidade->nome; // 'Belo Horizonte'
Then I want the ordering of records to start with users who have the same city and, after that, be indifferent.
SELECT * FROM usuarios JOIN cidade.id = usuarios.cidade_id
ORDER BY <cidade.nome COMEÇANDO DE $valor>
Fictional code - I know it would make a syntax error, it’s just a demonstration of what I want.
Is there any way to sort the data of a query, other than by the table field, but by predefined specified values?