在sql中查找最小值

时间:2016-11-01 06:52:33

标签: mysql sql

好的,所以这张桌子是关于一个慈善机构。它有三个表。 1)捐赠者2)礼物 和3)年

我必须找到2000年的最低值,显示捐赠者的名字和姓氏。我能够显示2000年的最低金额,但是捐赠者的名字不对。这就是我到目前为止所做的:

SELECT DONOR.DONOR_LNAME, DONOR.DONOR_FNAME, MIN( GIFT.AMOUNT ) 
FROM DONOR
JOIN GIFT ON DONOR.DONOR_NO = GIFT.DONOR_NO
WHERE YEAR_NUM =2000
HAVING MIN( GIFT.AMOUNT ) 
LIMIT 0 , 30

请帮忙

捐助者表

enter image description here

礼品表

enter image description here

3 个答案:

答案 0 :(得分:2)

SELECT t1.DONOR_FNAME AS first_name,
       t1.DONOR_LNAME AS last_name,
       t2.AMOUNT AS min_donation
FROM DONOR t1
INNER JOIN GIFT t2
    ON t1.DONOR_NO = t2.DONOR_NO
WHERE t2.YEAR_NUM = 2000 AND
      t2.AMOUNT = (SELECT MIN(AMOUNT) FROM GIFT WHERE YEAR_NUM = 2000 GROUP BY YEAR_NUM)

答案 1 :(得分:0)

将分组添加到您的查询中,但不确定限制事项......

SELECT DONOR.DONOR_LNAME, DONOR.DONOR_FNAME, MIN( GIFT.AMOUNT) AS MinAmount
FROM DONOR
JOIN GIFT ON DONOR.DONOR_NO = GIFT.DONOR_NO
WHERE YEAR_NUM =2000
GROUP BY DONOR.DONOR_LNAME, DONOR.DONOR_FNAME

答案 2 :(得分:0)

查看表结构后,您应该尝试按照查询。

SELECT DONOR.DONOR_NO, DONOR.DONOR_LNAME, DONOR.DONOR_FNAME, GIFT.AMOUNT
FROM DONOR INNER JOIN GIFT ON DONOR.DONOR_NO = GIFT.DONOR_NO
WHERE GIFT.YEAR_NUM =2000
HAVING MIN(GIFT.AMOUNT)
LIMIT 0, 30