我在一项作业中遇到了一个问题,我花了超过 40 个小时试图解决这个问题。将两个表连接在一起时,我得到了正确的答案,但是当我添加第三个时 - 我丢失了一行并且计数不正确。
这会找到正确的答案:
use prime_minister_2013;
select ministry.pm_name, count(*) AS Number_times_PM, ministry.min_begin, ministry.party,deputy_pm.deputy_name
from ministry, deputy_pm
where deputy_pm.min_nr = ministry.min_nr
and ministry.party <> 'ALP'
and min_begin < '1930-01-01'
group by ministry.pm_name;
but when I try to add the third table containing who was the Governor General when they are elected it loses one of the rows (a PM) and the count on 2 of the rows doubles.
我假设我的连接/& 或分组不正确!
use prime_minister_2013;
select ministry.pm_name, count(*) AS Number_times_PM, ministry.min_begin, ministry.party,deputy_pm.deputy_name, gg_title, gg_name
from ministry
join deputy_pm
on deputy_pm.min_nr = ministry.min_nr
join governor_general
on governor_general.pm_name = ministry.pm_name
and ministry.party <> 'ALP'
and min_begin < '1930-01-01'
group by ministry.pm_name;
非常感谢任何建议 - 25/9/13 到期
谢谢大家的评论 - 请在下面找到表格示例:
表:事工
min_nr pm_name party min _begin
1 Barton E Protectionist 1/01/1901
15 Bruce S M Nationalist 9/02/1923
27 Chifley J B ALP 13/07/1945
28 Chifley J B ALP 1/11/1946
9 Cook J Liberal 24/06/1913
24 Curtin J J A ALP 7/10/1941
25 Curtin J J A ALP 21/09/1943
表:副总理
min_nr deputy _ name party
1 Deakin A Protectionist
2 Lyne W J Protectionist
3 Hughes W M ALP
4 McLean A Protectionist
5 Isaacs I A Protectionist
6 Hughes W M ALP
7 Cook J Free Trade
8 Hughes W M ALP
9 Forrest J Liberal
表:总督
GG_name GG_title GG _begin GG _end pm_name
Hope J A L 7th Earl of Hopetoun 1/01/1901 9/01/1903 Barton E
Tennyson H 2nd Baron Tennyson 9/01/1903 21/01/1904 Barton E
Northcote H S Baron Northcote 21/01/1904 9/09/1908 Deakin A
Ward W H 2nd Earl of Dudley 9/09/1908 31/07/1911 Deakin A
Denman T 3rd Baron Denman 31/07/1911 18/05/1914 Fisher A
Munro Ferguson, R C Rt Hon Sir 18/05/1914 6/10/1920 Cook J
Forster H W Baron Forster 6/10/1920 8/10/1925 Hughes W M
Baird J L Baron Stonehaven 8/10/1925 22/01/1931 Bruce S M
Isaacs I A Rt Hon Sir 22/01/1931 23/01/1936 Scullin J H
Hore-Ruthven A G A Brig. Gen, Baron Gowrie 23/01/1936 30/01/1945 Lyons J A
Gloucester H W F A HRH Prince, Duke of Gloucester 30/01/1945 11/03/1947 Curtin J J A
为了回应 Space Dogs 的建议 - 我尝试了这个:
use prime_minister_2013;
select ministry.pm_name AS 'PM Name', DATE_FORMAT (ministry.min_begin,'%Y') As 'Ministry Started', ministry.party AS 'Party', deputy_pm.deputy_name AS 'Deputy PM Name', COUNT(DISTINCT ministry.min_begin) AS 'Number of times PM', gg_title AS 'GG Title', gg_name AS 'GG Name'
from ministry
join deputy_pm
on deputy_pm.min_nr = ministry.min_nr
join governor_general
on governor_general.pm_name = ministry.pm_name
and ministry.party <> 'ALP'
and min_begin < '1930-01-01'
group by ministry.pm_name;
这给了我:
Barton E 1901 Protectionist Deakin A 1 2nd Baron Tennyson Tennyson H
Bruce S M 1923 Nationalist Page C G 1 Baron Stonehaven Baird J L
Cook J 1913 Liberal Forrest J 1 Rt Hon Sir Munro Ferguson, R C
Deakin A 1909 Protectionist Cook J 3 Baron Northcote Northcote H S
Hughes W M 1916 National Labor Pearce G F 3 Baron Forster Forster H W
即将到达那里 - 但仍然缺少没有 GG 的 PM