我在两张桌子上进行左连接,但是生产笛卡尔产品而在其他桌子上没有生产。查询的样本结果可以在下面的图片中看到。
查询当前查询是:
SELECT
R.Region,
C.CountryName,
D.Year,
I.Income,
D.Completion_Rate AS 'Completion Rate',
D.Pupil_Teacher_Ratio AS 'Pupil-Teacher Ratio'
FROM
(SELECT
C.CountryCodeC AS 'CountryCode',
C.YearCC AS 'Year',
C.Completion_Rate,
R.Pupil_Teacher_Ratio
FROM
(SELECT
CountryCode AS 'CountryCodeC',
Data AS 'Completion_Rate',
YearC AS 'YearCC'
FROM
DataByYear
WHERE
SeriesCode = "SE.SEC.CMPT.LO.ZS"
AND YearC >= "2011%") C
LEFT JOIN
(SELECT
CountryCode AS 'CountryCodeR',
Data AS 'Pupil_Teacher_Ratio',
YearC AS 'YearCR'
FROM
DataByYear
WHERE
SeriesCode = "SE.SEC.ENRL.LO.TC.ZS"
AND YearC >= "2011%") R
ON C.CountryCodeC = R.CountryCodeR) D,
CountryRegion R,
Country C,
CountryIncome I
WHERE
R.CountryCode = D.CountryCode
AND R.CountryCode = C.CountryCode
AND I.CountryCode = D.CountryCode
问题是什么?我该如何解决?谢谢!
答案 0 :(得分:1)
我认为您还希望加入Year
以查询所有问题。如果您没有指定一年,那么每年都会加入所有其他年份并夸大您的价值观。所以你应该添加一个条件:
A.Year = B.Year
在整个查询过程中。
答案 1 :(得分:1)
尝试使用INNER JOIN
代替左连接。