简体   繁体   中英

Showing and sum data on php mysql in 1 table

I have problem with showing total order from db group by product title and how many times the product was ordered. The main problem is the code doesn't show the total ordered on each product, it only say 1 although on 1 product have 2 times ordered.

Field: id, user, title, telp, add, ordered

Record: Minimalist house type 80/110 -> 2 record and minimalist house type 100 -> 1 record.

All I want is how to show the data like:

id | title | ordered
1 | Minimalist house Type 80/110 | 2
2 | Minimalist house Type 100 | 1

but, the reality is:

1 | Minimalist house Type 80/110 | 1
2 | Minimalist house Type 100 | 1

Here my code:

$sql = mysql_query("SELECT id, title, ordered, sum(ordered) as totalordered FROM ordertbl GROUP BY title order by totalordered DESC");

$i = 1;

while($tampil = mysql_fetch_array($sql))
{
$data[$i]=array('NO'=>$i,'JUDUL'=>$tampil['title'],'DIBELI'=>$tampil['ordered']);

    $i++;
}

any help will so helpfull. Thanks

Try this:

SELECT id, title, count(ordered) as ordered, sum(ordered) as totalordered
FROM ordertbl GROUP BY title order by totalordered DESC

Remove the normal ordered column and replace it with count(ordered) so that proper count is displayed instead of particular order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM