Jump to content
rodigeva

CONSULTA RAND MYSQL

Recommended Posts

rodigeva

Quero fazer uma consulta MYSQL, e exibir ORDER BY RAND LIMIT 2.

Mas tenho duas divs e cada uma precisa receber um desses resultados.

Faço duas consultas?

Vlw, abraço.

FAZ A CONSULTA:

<?php
include ("conn.php");

$query = mysql_query("SELECT * FROM imoveis ORDER BY RAND() LIMIT 2");
while ($resultado = mysql_fetch_array($query)) {

$valor = $resultado["valor"];
$tipo = $resultado["tipo"];
$cidade = $resultado["cidade"];
$estado = $resultado["estado"];

?>

EXIBE OS RESULTADOS:
<tr>
<td align=center valign=middle class="tabela_corpo">
<?php echo $valor; ?>
</td>
<td align=center valign=middle class="tabela_corpo">
<?php echo $tipo; ?>
</td>
<td align=center valign=middle class="tabela_corpo">
<?php echo $cidade; ?>
</td>
<td align=center valign=middle class="tabela_corpo">
<?php echo $estado; ?>
</td>
</tr>
<?
}
?>

Share this post


Link to post
Share on other sites
KTachyon

Não deves utilizar o ORDER BY RAND(). Para tabelas com muitos dados gasta muitos ciclos. Gera 2 números aleatórios e faz 2 consultas:

select * from imoveis limit $x, 1;
select * from imoveis limit $y, 1;


“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.