ERD 和 SQL 查询帮助?

发布于 2024-09-13 04:52:28 字数 1096 浏览 2 评论 0原文

你能告诉我这样做是否正确,或者我是否需要改进其中的一些,当存在关联实体时,我很难理解查询。

ERD

列出所有捐赠者

SELECT* from Donor;

列出所有捐赠者的名字和姓氏

SELECT dfname, dlname
FROM donor

列出捐赠者号码 106 和 125 的电话号码 列出

SELECT dphone
FROM DONOR 
WHERE dphone = “106” 
AND dphone = “125”; 

每位捐赠者每年捐赠的金额

SELECT year 
FROM YEAR IN (
SELECT donor, amount
FROM GIFT);

列出每年捐赠的捐赠者

SELECT dfname, dlname 
FROM DONOR
WHERE IN( SELECT * FROM 
YEAR)
AND amount != null;

列出居住在的捐赠者姓名佐治亚州或北卡罗来纳州

SELECT dfname, dlname
FROM donor
WHERE state = “GA” 
OR  state = “NC”;

列出姓氏为威廉姆斯且居住在佐治亚州雅典的捐助者的姓名

SELECT dfname, dlname 
FROM donor 
WHERE dlname = “Williams”
AND city = “Athens”
AND state = “GA”;

我要感谢每一位提供帮助的人,我希望我能给每个人一个绿色复选标记,因为输入的所有答案都提供了解决方案。我感谢您的帮助,也许有一天我可以为您将来可能遇到的问题做出贡献。

Can you tell me if this is done right or if I need to improve some of them I have a hard time understanding the query's when there is an associative entity.

ERD

List all donors

SELECT* from Donor;

List the first and last names of all donors

SELECT dfname, dlname
FROM donor

List the phone numbers of donors number 106 and 125

SELECT dphone
FROM DONOR 
WHERE dphone = “106” 
AND dphone = “125”; 

List the amount given by each donor for each year

SELECT year 
FROM YEAR IN (
SELECT donor, amount
FROM GIFT);

List the donors who have made a donation every year

SELECT dfname, dlname 
FROM DONOR
WHERE IN( SELECT * FROM 
YEAR)
AND amount != null;

List the names of donors who live in Georgia or North Carolina

SELECT dfname, dlname
FROM donor
WHERE state = “GA” 
OR  state = “NC”;

List the names of donors whose last name is Williams and who live in Athens, GA

SELECT dfname, dlname 
FROM donor 
WHERE dlname = “Williams”
AND city = “Athens”
AND state = “GA”;

Id like to thank every one who helped and I wish I could give everyone a green check mark as all answers entered provided a solution. I appreciate the help and maybe one day I can contribute to a question you may have in the future.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老娘不死你永远是小三 2024-09-20 04:52:28

每年进行捐赠的捐赠者(警告:未测试):(

SELECT dfname, dlname
FROM   DONOR
WHERE NOT EXISTS 
   (SELECT * FROM YEAR WHERE NOT EXISTS 
      (SELECT * FROM GIFT WHERE year = YEAR.year AND donor = DONOR.donor));

假设 GIFT 中的一行表示已进行捐赠,即金额始终 > 0)

Donors who have made a donation every year (caveat: not tested):

SELECT dfname, dlname
FROM   DONOR
WHERE NOT EXISTS 
   (SELECT * FROM YEAR WHERE NOT EXISTS 
      (SELECT * FROM GIFT WHERE year = YEAR.year AND donor = DONOR.donor));

(Assuming that a row in GIFT means a donation was made, i.e. that amount > 0 always)

何以畏孤独 2024-09-20 04:52:28

在 ERD 中,您可能希望在 GIFT 表上放置一个主键。如果每个捐赠者每年只有一份礼物,那么主键可以是捐赠者和年份。否则添加人工密钥将是一个好主意。

列出捐赠者号码 106 和 125 的电话号码 - 我认为您想在 WHERE 子句中使用不同的字段。您应该找到这些捐赠者的电话号码 (dphone),但捐赠者 ID 不是电话号码。

列出每年捐款的捐助者 - 我认为您的查询不会满足您的要求。解决这个问题的一种方法是列出捐赠者名单以及他们捐赠的年份,然后找到他们没有捐赠的年份。在“未捐赠年份”列表中没有任何条目的捐赠者将是每年捐赠的捐赠者。提示:涉及使用外连接。

分享并享受。

In your ERD you might want to put a primary key on your GIFT table. If there's only one gift per donor per year then the primary key could be DONOR and YEAR. Otherwise adding an artificial key would be a good idea.

List the phone numbers of donors number 106 and 125 - I think you want to use a different field in the WHERE clause. You're supposed to find the phone number of these donors (dphone), but the donor ID is not the phone number.

List the donors who have made a donation every year - I don't think your query will do what you want. A way to approach this would be to produce a list of donors and the years that they gave, then find the years where they didn't give. Donors who don't have any entries in the "years they didn't give" list would be the ones that gave every year. HINT: involves using outer joins.

Share and enjoy.

最冷一天 2024-09-20 04:52:28

更正问题:

列出 106 号和 125 号捐款人的电话号码

SELECT dphone
FROM DONOR 
WHERE donor in (106, 125)

列出每位捐款人每年捐赠的金额

SELECT donor, year, sum(amount) as amount
FROM gift
group by donor, year

列出每年捐款的捐款人

这里需要澄清:什么是“每年”?

corrected queries:

List the phone numbers of donors number 106 and 125

SELECT dphone
FROM DONOR 
WHERE donor in (106, 125)

List the amount given by each donor for each year

SELECT donor, year, sum(amount) as amount
FROM gift
group by donor, year

List the donors who have made a donation every year

This should be clarified: What is mean "every year"?

一百个冬季 2024-09-20 04:52:28

列出每年捐款的捐助者

    Select dfname, dlastName 
     from
    Donor d
    INNER JOIN 
    (SELECT Donor, count(year) cyear
    FROM DONOR d
    INNER JOIN gift g
    on d.donor = g.donor
    GROUP BY donor
) donorYear
    ON d.donor = donorYear.donor
    INNER JOIN 
    (SELECT count(year) cyear
    from Year) years
    on years.cyear = donarYear.cyear

List the donors who have made a donation every year

    Select dfname, dlastName 
     from
    Donor d
    INNER JOIN 
    (SELECT Donor, count(year) cyear
    FROM DONOR d
    INNER JOIN gift g
    on d.donor = g.donor
    GROUP BY donor
) donorYear
    ON d.donor = donorYear.donor
    INNER JOIN 
    (SELECT count(year) cyear
    from Year) years
    on years.cyear = donarYear.cyear
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文