Using as an example, I have a certain table of users, where the city of that user is, is a reference to another table.
I need the order of these users in a query to be given according to the location of the current logged-in user.
For example, if I live in Belo Horizonte
, the user data of Belo Horizonte
must first be displayed in this query.
I know that in MySQL ORDER BY
works as a sort order by the present value in the column, but I do not know how I can sort by specific values.
For example, I want the order to be: Belo Horizonte
and then it makes no difference.
In a fictitious example I'll show you what I want:
First I get the value of the city:
$valor = Auth::user()->cidade->nome; // 'Belo Horizonte'
Then I want the ordering of records to begin 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>
Code fiction - I know it would give syntax error, it's just the demonstration of what I want.
Is there any way to sort the data of a query, not by the table field, but by specified default values?