我正在NGO数据库中工作,那里有很多表和许多列,现在我停留在每种性别类型(m =男性,w =女性,c =儿童)的计算/总缴费额上
以下是我的表及其列
Table1 "basic_detail"
+---+------------------+------+
|id | name | m_w_c
----+------------------+------+
|1 |jhon |m
|2 |sara |w
|3 |mike |c
|4 |simon |m
|5 |frank |c
|6 |suzi |w
"and so on more than 1000 entries"
Table2 "contribution" (c_id= contribution table_id auto incremental) (bd_id= basic_detail table_id)
+------+------+--------+-----------+-----------+
|c_id |bd_id | books | uniform | food |
+------+------+--------+-----------+-----------+
|1 | 1 | 50 | 40 | 0 |
|2 | 3 | 0 | 0 | 5 |
|3 | 2 | 50 | 45 | 0 |
|4 | 5 | 2 | 4 | 3 |
|5 | 4 | 60 | 75 | 0 |
|6 | 6 | 35 | 50 | 10 |
and so on more than 1000 entries equals to basic_detail table
我想分别计算男人,女人和孩子的捐款,
如果孩子只能贡献食物,如果孩子贡献书籍和校服,则此贡献应计入c_contribution,其中m = men,w = women仅能贡献书籍和校服食品中,以下金额应计入c_contribution,
如果您不满意,请大家在我的问题上帮助我,请不要标记“-”,谢谢。
答案 0 :(得分:0)
像这样吗?
$men = $women = child = c_contribution = 0;
$contributions = $pdo->query("
SELECT c.books, c.uniform, c.food, bd.m_w_c
FROM contribution c
LEFT JOIN basic_detail bd ON c.bd_id = bd.id
")->fetchAll();
foreach($contributions as $contribution) {
if ( $contribution['m_w_c'] == 'c' ) {
$c_contribution += $contribution['books'];
$c_contribution += $contribution['uniform'];
$child += $contribution['food'];
} else {
$men += $contribution['books'];
$women += $contribution['books'];
$men += $contribution['uniform'];
$women += $contribution['uniform'];
$c_contribution += $contribution['food'];
}
}
print("Men Total: $men");
print("Women Total: $women");
print("Child Total: $child");
print("Combined Total: $c_contribution");
答案 1 :(得分:0)
此代码可帮助我查看各个领域中男性,女性,儿童贡献的总和,但是我仍然无法添加男性或女性在食物中贡献的总和。
require "connpdo.php";
$connection = new PDO($dsn, $username, $password, $options);
$men = $women = $child = $c_contribution = 0;
$contributions = $connection->query("
SELECT c.books, c.uniform, c.food, bd.m_w_c
FROM contribution c
LEFT JOIN basic_detail bd ON c.bd_id = bd.id
")->fetchAll();
foreach($contributions as $contribution) {
if ( $contribution['m_w_c'] == 'w' ) {
$women += $contribution['books'];
$women += $contribution['uniform'];
} elseif ( $contribution['m_w_c'] == 'm' ) {
$men += $contribution['books'];
$men += $contribution['uniform'];
} elseif ( $contribution['m_w_c'] == 'c' ) {
$child += $contribution['books'];
$child += $contribution['uniform'];
$child += $contribution['food'];
}
}
print("Men Total: $men");
print("Women Total: $women");
print("Child Total: $child");
print("Combined Total: $c_contribution");
?>
请...