ERD 和 SQL 查询帮助?
你能告诉我这样做是否正确,或者我是否需要改进其中的一些,当存在关联实体时,我很难理解查询。
列出所有捐赠者
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每年进行捐赠的捐赠者(警告:未测试):(
假设 GIFT 中的一行表示已进行捐赠,即金额始终 > 0)
Donors who have made a donation every year (caveat: not tested):
(Assuming that a row in GIFT means a donation was made, i.e. that amount > 0 always)
在 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.
更正问题:
列出 106 号和 125 号捐款人的电话号码
列出每位捐款人每年捐赠的金额
列出每年捐款的捐款人
这里需要澄清:什么是“每年”?
corrected queries:
List the phone numbers of donors number 106 and 125
List the amount given by each donor for each year
List the donors who have made a donation every year
This should be clarified: What is mean "every year"?
列出每年捐款的捐助者
List the donors who have made a donation every year