1
Good afternoon, Is there any method in an sql query to sort from a value and if there is this value return it from first and then all the rest of the values ?
Example : A table with days from 1 to 7, where I would put in case it existed return the value 5 first and then all others ( "123467")
Would almost be a Where but that I could pull the other values also after this selected value.
$dia = 5;
$query = $this->db->query("SELECT * FROM tabela where dia = $dia");
//Teria que retornar query com o primeiro valor = 5 e depois todos outros valores.
Just make a
ORDER BY dia != 5, dia
, in your case,ORDER BY dia != $dia, dia
. Anything that runs away from that, complicates it for nothing, and you still run the risk of not taking advantage of the ratings. The explanation is in the original post linked above (attention to the!=
, which is false to be sorted as less than true). If you want, make a FIDDLE SQL with some sample data, which I demonstrate the query to you.– Bacco
By the way, I had already made a fiddle in 2016, I didn’t even remember (it’s in the original post). Test here: http://sqlfiddle.com/#! 9/52a4c3/1 - The difference of the example there is that as the second criterion we are using another field.
– Bacco
Thank you @Bacco
– Vinicius Gularte