MySQL,SubSelect,这可能吗?

时间:2012-11-22 17:03:48

标签: php mysql

所以,我有一张捐款表。它存储$ value和列框,他们选择货币或实物捐赠。实物捐赠可以有$但通常他们没有。如果有人捐赠了物理物品,割草机,桌子,椅子等,那么实物捐赠就是如此。然后我有捐助者。最后,我有一个领导力捐赠小组。根据一定数额,捐赠者可以进入领导捐赠小组。因此,100美元的捐赠者被安排在其他捐赠者组中,1000美元他们被放入下一个捐赠组中,依此类推。我需要总结所有捐款,但不包括所有实物捐赠,因为那些(即使它们具有货币价值)不计算捐赠者在领导小组的位置。

TABLES:我无法进行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,它是每个领导小组的名称:其他捐赠者,创始人俱乐部,朋友圈等等。这将永远是唯一的,所以我把它作为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') ?>

在旁注中,我昨天问了一个问题,并在这个漂亮的小方框中给出了回复,显示了一个表名和所有表列标题。有没有办法可以在问题中做到这一点,所以我可以更好地说明一点,让我的问题更清楚?

谢谢大家,

乔尔

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

如果我理解得很好,问题是:为每个用户总结所有“非实物”捐款,以确定他们应该添加哪个群组。

如果是这样,您的数据库模型缺少一些外键,以便将捐赠者与他们的捐款和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

你最终会向每个捐赠者提供他们以真钱捐赠的金额。