1

所以,我有一个捐赠表。它存储了一个 $ 值和一个列框,他们可以在其中选择货币或实物捐赠。实物捐赠可以有美元,但通常没有。实物捐赠是指有人捐赠了实物,例如割草机、桌子、椅子等。然后我有捐助者。最后,我有一个领导力捐赠小组。根据一定的金额,捐赠者会被放入其中一个领导力捐赠小组。因此,100 美元的捐赠者被放在其他捐赠者组中,1000 美元的捐赠者被放在下一个捐赠者组中,依此类推。我需要汇总所有捐赠,但排除所有实物捐赠,因为这些(即使它们具有货币价值)不计入将捐赠者置于领导小组中的哪个位置。

表格:我无法执行 SHOW CREATE,因为它有一个需要更改的服务器值,我无法控制。我希望这对每个人都好。我只是想显示 3 个表及其所有当前字段。很抱歉给您带来不便。

好的,所以对于表格,我不记得所有的语法,但应该很容易看到外键和主键以及所有字段。我只是有点匆忙,因为这是感恩节,我的家人因为我在电脑上而生气。

CREATE TABLE donor (
DonorID INT NOT NULL AUTO_INCREMENT Primary Key,
FirstName VARCHAR(100),
LastName VARCHAR(100),
Street Address VARCHAR(100),
City VARCHAR(100),
State VARCHAR(2),
Zip VARCHAR(5),
Email VARCHAR(100), );

限制是他们通过下拉框选择的两个条目之一。受限(然后由捐助者为特定项目指定或非受限意味着捐助者不关心资金的使用方式。DonationType 的工作方式相同,但它是实物或货币。我只想总结一下货币价值

CREATE TABLE donation (
DonationID INT NOT NULL AUTO_INCREMENT Primary Key,
Restriction VARCHAR(10) NOT NULL,
Amount VARCHAR(100) NOT NULL,
Description VARCHAR(2000),
DonationType VARCHAR(100) NOT NULL,
DonorID INT NOT NULL Foreign Key,
DateReceived DATE NOT NULL,

领导小组表:我吃完再补充,对不起,我的家人要杀了我。

LeaderGrp 不是 AI,它是每个领导小组的名称:Other Donor、Founder's Club、Friend's Circle 等等。这将永远是独一无二的,所以我把它做成了 PK,但如果出于某种原因和 AI 领域我可以做到这一点。

如果这样会更容易,我可以发布我的代码,但我觉得它会变成人们的巨大文本墙。然而,这是一个网站,我在这个项目中使用的其他编程语言是 js 和 php,所以如果它可以在 php 中使用也是一个选项。我也可以给你网站的链接,让你查看信息,因为目前都是编造的,如果有帮助的话,显然不能分享真实的数据。只是要求它,因为当有些人可能只是进入并开始向我的表中添加数据时,我真的不想发布它。

代码:

<?php require_once('header.php') ?>
<!--dont modify above this line-->

<?php 
//on ANY page you need to use the database, you need to include the line below ONCE (before doing any db operations)
require_once('connect.php');

function checkAmount($amount, $min, $max)
{
if ($amount >= $min && $amount <= $max)
return true;
else
return false;

} 


//array types of users
$founders_club = array();
$headmasters_circle = array();
$dragons_circle = array();
$green_and_white_society = array();
$seventies_society = array();
$millennium_society = array();
$dome_society = array();
$tunnel_society = array();
$friends_circle = array();
$others = array();

// fiscal year
$currentyear = date("Y");
$selectedyear = $_POST["selectedYear"];
if($selectedyear == "")
$selectedyear = $currentyear;
$startdate = $selectedyear . "-07-01";
$enddate = $selectedyear + 1 . "-06-30";

//we need to put all donors inside the dropdown box, this is how to do it
//the actual query
$result = mysql_query("SELECT * FROM donor;") or die (mysql_error());
echo "<h2>Publication List</h>";
echo "<div>Choose Fiscal Year:";
//this will iterate through every record in the resultset and create an option box
echo "<form id=\"fiscal\" action=\"q1.php\" method=\"POST\">
<select name=\"selectedYear\" onchange=\"document.getElementById('fiscal').submit();\">";
for($i=$currentyear; $i >= 2011; $i--)
{
    if ($i == $selectedyear)
    echo "<option value='".$i."' selected=\"selected\">".$i."</option>";
    else
    echo "<option value='".$i."'>".$i."</option>";
}
echo "</select></form>";
echo "</div><br>";
//this will iterate through every record in the resultset and create an option box
while($row = mysql_fetch_array($result))
{

$result2 = mysql_query("SELECT SUM(DonationAmount) FROM donations Where DonorID = ".$row['DonorID'] . " and donations.Date_Received BETWEEN '" . $startdate . "' and '" . $enddate . "'" ) or die (mysql_error());
    while($row2 = mysql_fetch_array($result2))
        {
        $amount = $row2['SUM(DonationAmount)'];         

        if (checkAmount($amount, 50000, 999999))    
        array_push($founders_club, $row['DisplayName']);

        if (checkAmount($amount, 25000, 49999))
        array_push($headmasters_circle, $row['DisplayName']);

        if (checkAmount($amount, 10000, 24999))
        array_push($dragons_circle, $row['DisplayName']);

        if (checkAmount($amount, 5000, 9999))
        array_push($green_and_white_society, $row['DisplayName']);

        if (checkAmount($amount, 2500, 4999))
        array_push($seventies_society, $row['DisplayName']);

        if (checkAmount($amount, 1000, 2499))
        array_push($millennium_society, $row['DisplayName']);

        if (checkAmount($amount, 500, 999))
        array_push($dome_society, $row['DisplayName']);

        if (checkAmount($amount, 250, 499))
        array_push($tunnel_society, $row['DisplayName']);

        if (checkAmount($amount, 100, 249))
        array_push($friends_circle, $row['DisplayName']);

        if (checkAmount($amount, -1, 99))
        array_push($others, $row['DisplayName']);

        }

}


        //display results
    if(count($founders_club) > 0)   
    {
    echo "<h2>Founder's Club</h2>";
    for ($i = 0; $i < count($founders_club); $i++) 
    echo $founders_club[$i] . "<br>";
    }

    if(count($headmasters_circle) > 0)  
    {
    echo "<h2>Headmaster's Circle</h2>";
    for ($i = 0; $i < count($headmasters_circle); $i++) 
    echo $headmasters_circle[$i] . "<br>";
    }

    if(count($dragons_circle) > 0)  
    {
    echo "<h2>Dragon's Circle</h2>";
    for ($i = 0; $i < count($dragons_circle); $i++) 
    echo $dragons_circle[$i] . "<br>";
    }

    if(count($green_and_white_society) > 0) 
    {
    echo "<h2>Green and White Society</h2>";
    for ($i = 0; $i < count($green_and_white_society); $i++) 
    echo $green_and_white_society[$i] . "<br>";
    }

    if(count($seventies_society) > 0)   
    {
    echo "<h2>1970's Society</h2>";
    for ($i = 0; $i < count($seventies_society); $i++) 
    echo $seventies_society[$i] . "<br>";
    }

    if(count($millennium_society) > 0)  
    {
    echo "<h2>Millennium Society</h2>";
    for ($i = 0; $i < count($millennium_society); $i++) 
    echo $millennium_society[$i] . "<br>";
    }

    if(count($dome_society) > 0)    
    {
    echo "<h2>Dome Society</h2>";
    for ($i = 0; $i < count($dome_society); $i++) 
    echo $dome_society[$i] . "<br>";
    }

    if(count($tunnel_society) > 0)  
    {
    echo "<h2>Tunnel Society</h2>";
    for ($i = 0; $i < count($tunnel_society); $i++) 
    echo $tunnel_society[$i] . "<br>";
    }

    if(count($friends_circle) > 0)  
    {
    echo "<h2>Friends' Circle</h2>";
    for ($i = 0; $i < count($friends_circle); $i++) 
    echo $friends_circle[$i] . "<br>";
    }

    if(count($others) > 0)  
    {
    echo "<h2>Other Donors</h2>";
    for ($i = 0; $i < count($others); $i++) 
    echo $others[$i] . "<br>";      
    }

?>







<!--dont modify below this line-->
<?php require_once('footer.php') ?>

顺便说一句,我昨天问了一个问题,在这个漂亮的小盒子里给了我回复,这个小盒子显示了一个表名和所有表列标题。有没有办法可以在问题中做到这一点,以便我可以更好地说明并使我的问题更清楚?

谢谢大家,

乔尔

4

2 回答 2

2

这应该可以满足您的需求。

mysql> select * from donation;
+------------+--------------+----------------+---------+
| DonationID | DonationType | DonationAmount | DonorID |
+------------+--------------+----------------+---------+
|          1 | monetary     |             50 |       6 |
|          2 | monetary     |            100 |       5 |
|          3 | monetary     |             25 |       5 |
|          4 | monetary     |             75 |       7 |
|          5 | monetary     |            250 |       8 |
|          6 | inkind       |             13 |       1 |
|          7 | inkind       |             19 |      15 |
|          8 | inkind       |              0 |       1 |
|          9 | inkind       |              0 |      23 |
|         10 | inkind       |              0 |       1 |
|         11 | inkind       |           4000 |       3 |
+------------+--------------+----------------+---------+
11 rows in set (0.00 sec)

mysql> select DonorID,sum(DonationAmount) as Total from donation where DonationType='monetary' group by DonorID order by Total Desc;
+---------+-------+
| DonorID | Total |
+---------+-------+
|       8 |   250 |
|       5 |   125 |
|       7 |    75 |
|       6 |    50 |
+---------+-------+
4 rows in set (0.00 sec)

mysql> 

然后,您可以更进一步,将您的捐赠表与您的捐赠者表连接起来,以在同一查询中获取捐赠者姓名。

mysql> select * from donors;
+---------+-------+
| DonorID | Name  |
+---------+-------+
|       6 | bob   |
|       5 | jack  |
|       7 | tim   |
|       8 | jane  |
|       1 | sara  |
|      15 | randy |
|       3 | hamed |
|      23 | jesse |
+---------+-------+
8 rows in set (0.00 sec)


mysql> select donation.DonorID,name,sum(DonationAmount) as Total from donation join donors on donation.DonorID=donors.DonorID where DonationType='monetary' group by DonorID order by Total Desc;
+---------+------+-------+
| DonorID | name | Total |
+---------+------+-------+
|       8 | jane |   250 |
|       5 | jack |   125 |
|       7 | tim  |    75 |
|       6 | bob  |    50 |
+---------+------+-------+
4 rows in set (0.00 sec)
于 2012-11-22T18:27:22.337 回答
1

如果我理解得很好,问题是:将每个用户的所有“非实物”捐赠相加,以确定他们应该添加到哪个组中。

如果是这样,您的 DB 模型缺少一些外键,以便将捐赠者与他们的捐赠和 LGG 联系起来。

这是您可以使用的示例 MCD:

Donor ((PK)DonorId, Address, Phone, (FK -> LGG) LggId)
Donation((PK)DonationId, (FK -> Donor)DonorId, DonationType, DonationAmount, Description, Date_Received)
LGG ((PK)LggId, LeaderGrp, LGDescrip, MinAmount)

其中 MinAmount 是属于该组所需的最低金额。

从那里,您所要做的就是:

select d.donorid, sum(do.donationamount)
from donor d,
donation do
where d.donorid=do.donorid
and do.donationtype!='in-kind'
group by d.donorid

你最终会得到每个捐赠者的真金白银金额。

于 2012-11-22T17:46:40.290 回答