Jump to content
Rila_xp

Problema select cu order si limit

Recommended Posts

Posted (edited)

Am tabelul statistics cu id,date si vizite.Vreau sa imi afiseze numarul total de vizite din ultimele 4 zile de exemplu si am incercat urmatorul cod si nu mi-a mers.

Ce gresesc?

$query = mysql_query("SELECT SUM(vizite) FROM statistics ORDER BY date DESC LIMIT 0,4", $con);

// Print out result
while($rowz = mysql_fetch_array($query)){
$sumz = $rowz['SUM(vizite)'];
}
echo "This week $sumz <br>";

Edited by Rila_xp
Posted

Incearca cu asta, presupunand ca ai cate un rand pentru fiecare zi (asa am inteles din ce am citit)


$sql = mysql_query('SELECT SUM(`vizite`) as `total_vizite` FROM `statistics` ORDER BY `id` DESC LIMIT 4') or die (mysql_error());
$fetch = mysql_fetch_array($sql);

echo $fetch[total_vizite];

Posted

^^Nu imi da nici o eroare,doar ca imi returneaza o valoare gresita(in loc sa imi ia ultimele 4 zile si sa imi adune,imi aduna pe toate zilele)

^la fel face si codul dat de tine

Structura tabelului am spus de la inceput,tabelul statistics cu campurile id,date,vizite pentru fiecare zi am un nou rand care contine id data si numarul de vizite

Posted

sau poti face asa :


$query = mysql_query("SELECT vizite FROM statistics ORDER BY date DESC LIMIT 0,4");

// Print out result
while($rowz = mysql_fetch_array($query)){
$sumz = $rowz['vizite'];
$suma = $sumz + $suma;
}
echo "This week $suma <br>";
?>

Posted

Am rezolvat asa:


$query=mysql_query("SELECT sum(vizite) FROM (SELECT vizite FROM statistics ORDER BY date DESC LIMIT 4) AS subquery", $con);
while($row = mysql_fetch_array($query)){
$sum = $row['sum(vizite)'];
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...