Select random with Sqlserver (mssql)

Asked

Viewed 71 times

3

What is the correct way to make an unspecified (random) selection using PHP + Sqlserver, since Microsoft queries do not accept ORDER BY RAND() from MYSQL.

Using MYSQLI would be like this:

$sql ="SELECT * FROM Tabela where campo=‘algum'  order by rand()”;

2 answers

3


0

Try to load the random index before and assign it to the string of the command.

<?php

$indiceRandomico = rand(5, 15);

$sql ="SELECT * FROM Tabela where campo=‘algum'  order by ”. $indiceRandomico;

?>
  • 1

    This is not gonna work.

  • If you restrict where you are by the number of columns, it should work.

  • 2

    But sorting by a random column is not the same thing as randomly sorting.

  • I understand your explanation.

  • 1

    Thank you very much, but unfortunately it didn’t even work for the number of columns. It worked with ORDER BY NEWID()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.