简体   繁体   English

在PHP中通过id mysql求和

[英]sum by id mysql in PHP

i want to sum all total_harga by id_subkategori, how i caan get that? 我想通过id_subkategori对所有total_harga求和,我怎么能得到? so just get for example all id_subkategori 5 get sum my database table 所以只获取例如所有id_subkategori 5求和我的数据库表

You can use GROUP BY id_subkategori to get the desired output. 您可以使用GROUP BY id_subkategori获得所需的输出。

select sum(total_harga) total, id_subkategori
from yourtable
group by id_subkategori

You can read more on aggregate functions here 您可以在此处阅读有关聚合函数的更多信息

Edit: 编辑:

If you want to pick for a specific id_subkategori, you need to change it like following. 如果要选择一个特定的id_subkategori,则需要像下面这样进行更改。

select sum(total_harga) total, id_subkategori
from yourtable
where id_subkategori=5
group by id_subkategori

Use a sql statement to sum the total. 使用sql语句求和。

select sum(total_harga) total
from yourtable
where id_subkategori = 5
group by id_subkategori

Then fetch the sql result by PHP. 然后通过PHP获取sql结果。 Please check this for your reference. 请检查作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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