Jump to content

error on sql select limit


funbinod
 Share

Recommended Posts

why these lines are not working?

 

$sql3 = "SELECT *,sum(amt),sum(qty) FROM purchase WHERE sid=$sid AND pvn!=0 ORDER BY pvn DESC LIMIT 2";$result3 = mysqli_query($connect, $sql3) or die(mysqli_error($connect));$row3 = mysqli_fetch_array($result3);echo $row3['sum(qty)'] . "<br />";$sql4 = "SELECT *,sum(amt),sum(qty) FROM purchase WHERE sid=$sid AND pvn!=0 ORDER BY pvn DESC LIMIT 3";$result4 = mysqli_query($connect, $sql4) or die(mysqli_error($connect));$row4 = mysqli_fetch_array($result4);echo $row4['sum(qty)'] . "<br />";

they both SELECT result from the complete rows on 'purchase' table. please guide what is wrong in it!

 

Link to comment
Share on other sites

if you can use sum syntax your can use Group and declare field of table don't use select * .Example :

Select sid,pvn ,item_name,sum(amt) as amt ,sum(qty) as Qty

From purchase

Where sid=$sid AND pvn!=0

Group by sid,item_name,pvn

ORDER BY pvn DESC LIMIT 3";

 

sorry my spelling bad.

Link to comment
Share on other sites

thanks for the reply. its ok for the spelling! but the problem is still the same. its not retrieving sum from last 3 rows. its summing from complete rows.... i wish to add qty from the declared LIMIT...

Link to comment
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
 Share

×
×
  • Create New...