简体   繁体   English

MySQL,SubSelect,这可能吗?

[英]MySQL, SubSelect, is this possible?

So, I have a donation table. 所以,我有一个捐款表。 It stores a $ value and a column box where they choose monetary or in kind donation. 它存储$ value和列框,他们选择货币或实物捐赠。 in-kind donations can have a $ but usually they don't. 实物捐赠可以有$但通常他们没有。 An in-kind donation would be if someone donated a physical item ex, lawnmower, table, chair, so on. 如果有人捐赠了物理物品,割草机,桌子,椅子等,那么实物捐赠就是如此。 I then have donors. 然后我有捐助者。 Finally, I have a Leadership Giving Group. 最后,我有一个领导力捐赠小组。 Based on certain amounts a donor gets put into one of the Leadership Giving Groups. 根据一定数额,捐赠者可以进入领导捐赠小组。 So, for $100 the donor is put in Other Donor group, for $1000 they are put in the next one up and so on. 因此,100美元的捐赠者被安排在其他捐赠者组中,1000美元他们被放入下一个捐赠组中,依此类推。 I need to sum all the donations but exclude all in kind donations because those(even if they have a monetary value) don't count for where to place the donor in the Leadership Group. 我需要总结所有捐款,但不包括所有实物捐赠,因为那些(即使它们具有货币价值)不计算捐赠者在领导小组中的位置。

TABLES: I wasn't able to do SHOW CREATE because it has a server value that needs to be changed that I don't control. TABLES:我无法进行SHOW CREATE,因为它有一个需要更改的服务器值,我无法控制。 I hope this is ok for everyone. 我希望每个人都可以。 I just wanted to show the 3 tables and all their current fields . 我只是想显示3个表及其所有当前字段。 Sorry for the inconvience. 很抱歉给您带来不便。

Ok, so for the tables I couldn't remember all the syntax but it should be easy enough to see the foreign key and Primary Key and all the fields are there. 好的,所以对于表格我不记得所有的语法,但它应该很容易看到外键和主键和所有字段都在那里。 I just am in a bit of a rush as it is Thanksgiving and my family is mad at me for being on the computer. 我感到很匆忙,因为这是感恩节,我的家人因为在电脑上而生我的气。

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), );

Restriction is 1 of two entries that they select via drop box. 限制是他们通过投递箱选择的两个条目中的一个。 Restricted(then it is been designated by donor for a specific project or Non-Restricted meaning donor doesn't care how the money is used. DonationType works the same way but it is either In-Kind or Monetary. I want to sum just the monetary values 受限制(然后它被捐赠者指定用于特定项目或非限制性意义捐赠者并不关心如何使用这些资金.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,

LeadershipGroup Table: I will add when I get done eating, sorry but my family is about to kill me. 领导小组表:当我吃完饭后,我会补充,抱歉,但我的家人即将杀了我。

LeaderGrp is not a AI, it is a name of each Leaderhip Group: Other Donor, Founder's Club, Friend's Circle, and so on. LeaderGrp不是AI,它是每个Leaderhip Group的名称:其他捐赠者,创始人俱乐部,朋友圈等等。 This will always be unique so I made it the PK but if for some reason and AI field I can do that. 这将永远是独一无二的,所以我把它作为PK,但如果由于某种原因和AI领域,我可以做到这一点。

I can post my code if that would make it easier but I feel as though it will turn into a huge wall of text for people. 我可以发布我的代码,如果这会使它更容易,但我觉得它将变成一个巨大的文本墙为人们。 However, this is a website and other programming languages I have used in this project are js and php so if it could be possible in php that is an option as well. 但是,这是一个网站和我在这个项目中使用的其他编程语言是js和php,所以如果它可以在PHP中也可以选择。 I could also give you the link the website and let you view the information because currently it is all made up, obviously the real data can't be shared if that would be helpful. 我也可以给你链接网站,让你查看信息,因为目前它已经全部组成,显然真正的数据无法共享,如果这将有所帮助。 Just ask for it because I didn't really want to post it when some people might just go in and start adding data to my tables. 只是要求它,因为当有些人可能只是进入并开始向我的表添加数据时,我真的不想发布它。

Code: 码:

<?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') ?>

On a side note, I asked a question yesterday and the response was given to me in this nice little box that showed a table name and all of table column headers. 在旁注中,我昨天问了一个问题,并在这个漂亮的小方框中给出了回复,显示了一个表名和所有表列标题。 Is there a way I can do that in the question so I can maybe illustrate a little better and make my question clearer? 有没有办法可以在问题中做到这一点,所以我可以更好地说明一点,让我的问题更清楚?

Thanks everyone, 感谢大家,

Joel 乔尔

This should do what you're looking for. 这应该做你想要的。

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> 

you can then take it a step further and join your donation table with your donors table to get the donor name in the same query. 然后,您可以更进一步,将您的捐款表与捐赠者表联系起来,以便在同一查询中获取捐赠者姓名。

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)

If I understand well, the question is : sum all the "not in-kind" donations for every user to determine in which group they should be added. 如果我理解得很好,问题是:为每个用户总结所有“非实物”捐款,以确定他们应该添加哪个组。

If so, your DB model lacks some foreign keys in order to link donors to their donations, and to LGGs. 如果是这样,您的数据库模型缺少一些外键,以便将捐赠者与捐赠和LGG联系起来。

Here's an example MCD you could use : 这是您可以使用的示例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)

Where the MinAmount is the minimum amount needed to belong to this group. MinAmount是属于该组所需的最小数量。

From there, all you have to do is : 从那里,你所要做的就是:

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

You end up with every donor with the amount they donated in real money. 你最终会得到每个捐赠者他们用真钱捐赠的金额。

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

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