2016-11-18 83 views
1

我的同學和我一直運行猖獗試圖讓此查詢正常工作。MySQL左連接正確加入工會返回不正確的數據

目標:返回一個由兩個表組成的表,該表由兩個表通過左連接和右連接完全連接而成。將會有空值。此表的目的是爲了跟蹤速度與重量如何一致,在左側Y軸上以速度和右側重量在Y軸上繪製速度和重量。有些值會有共享日期。

的假設:

  • 玩家不能在一天他們的體重多次記錄。
  • 玩家在一天內不能有多個衝刺速度條目。
  • 沒有日期可以獨立於至少一個衝刺值或權重存在。他們必須至少有一個與之相關的值。

的問題:幾個行返回值時,他們應該爲NULL,而是和我們沒有任何線索,其中該數據的來源。

電流不正確的查詢:

SELECT weight.username, weight.weight, metric.speedOrDistance, weighInDate 
FROM playerweight as weight LEFT JOIN sprint AS metric ON 
weight.weighInDate = metric.workoutDate 
WHERE weight.username = 'testuser' 
UNION 
SELECT metric.username, weight.weight, 
metric.speedOrDistance, workoutDate 
FROM playerweight as weight RIGHT 
JOIN sprint AS metric ON weight.weighInDate = metric.workoutDate 
WHERE metric.username = 'testuser' 
ORDER BY weighInDate; 

實施例的數據:

衝刺表

+----------+-------------+-----------------+ 
| username | workoutDate | speedOrDistance | 
+----------+-------------+-----------------+ 
| jdoe  | 2016-10-10 |   2.00 | 
| jdoe  | 2016-10-17 |   3.50 | 
| jdoe  | 2016-10-24 |   3.00 | 
| jdoe  | 2016-11-01 |   5.00 | 
| jdoe  | 2016-11-10 |   4.00 | 
| foo  | 2016-11-03 |   2.50 | 
| foo  | 2016-11-14 |   5.00 | 
| foo  | 2016-11-16 |   4.00 | 
| foo  | 2016-11-18 |   3.00 | 
| testuser | 2016-10-11 |   3.40 | 
| testuser | 2016-10-17 |   3.40 | 
| testuser | 2016-11-13 |   1.50 | 
| testuser | 2016-11-15 |   2.00 | 
| testuser | 2016-11-17 |   4.00 | 
+----------+-------------+-----------------+ 

playerWeight

+----------+-------------+--------+ 
| username | weighInDate | weight | 
+----------+-------------+--------+ 
| jdoe  | 2016-10-10 | 160 | 
| jdoe  | 2016-10-17 | 160 | 
| jdoe  | 2016-10-24 | 170 | 
| jdoe  | 2016-11-01 | 180 | 
| jdoe  | 2016-11-08 | 180 | 
| jdoe  | 2016-11-09 | 200 | 
| jdoe  | 2016-11-11 | 178 | 
| jdoe  | 2016-11-22 | 195 | 
| foo  | 2016-11-01 | 190 | 
| foo  | 2016-11-10 | 185 | 
| foo  | 2016-11-14 | 175 | 
| foo  | 2016-11-18 | 180 | 
| bar  | 2011-01-16 | 170 | 
| bar  | 2011-07-16 | 177 | 
| bar  | 2011-09-16 | 169 | 
| testuser | 2016-11-04 | 150 | 
| testuser | 2016-11-08 | 200 | 
| testuser | 2016-11-11 | 195 | 
| testuser | 2016-11-14 | 175 | 
| testuser | 2016-11-17 | 180 | 
+----------+-------------+--------+ 

正確結果表

+----------+--------+-----------------+-------------+ 
| username | weight | speedOrDistance | weighInDate | 
+----------+--------+-----------------+-------------+ 
| testuser | NULL | 3.4    | 2016-10-11 | 
| testuser | 160 | 3.4    | 2016-10-17 | -- ERROR ROW, weight should not have a value 
| testuser | 150 | NULL   | 2016-11-04 | 
| testuser | 200 | NULL   | 2016-11-08 | 
| testuser | 195 | NULL   | 2016-11-11 | 
| testuser | NULL | 1.5    | 2016-11-13 | 
| testuser | 175 | 5    | 2016-11-14 | -- ERROR ROW, speedOrDistance should not have a value 
| testuser | NULL | 2    | 2016-11-15 | 
| testuser | 180 | 4    | 2016-11-17 | 
+----------+--------+-----------------+-------------+ 

理想的結果表

+----------+--------+-----------------+-------------+ 
| username | weight | speedOrDistance | weighInDate | 
+----------+--------+-----------------+-------------+ 
| testuser | NULL | 3.4    | 2016-10-11 | 
| testuser | NULL | 3.4    | 2016-10-17 | 
| testuser | 150 | NULL   | 2016-11-04 | 
| testuser | 200 | NULL   | 2016-11-08 | 
| testuser | 195 | NULL   | 2016-11-11 | 
| testuser | NULL | 1.5    | 2016-11-13 | 
| testuser | 175 | NULL   | 2016-11-14 | 
| testuser | NULL | 2    | 2016-11-15 | 
| testuser | 180 | 4    | 2016-11-17 | 
+----------+--------+-----------------+-------------+ 

任何幫助,你們可以提供將不勝感激。我們不知道爲什麼這不起作用。

謝謝!!!

+1

聽起來就像使用'FULL JOIN'而不是'UNION','LEFT JOIN'和'RIGHT JOIN'。 – Nicarus

+0

Mysql不支持完整連接。 –

+0

我剛測試過這個,無法確認這些錯誤的行。我想,在你的測試環境中,你有不同的示例數據。 –

回答

3

您在workoutDate = weighInDate上進行連接。這爲您提供了無關行的組合,這些行仍保留在結果集中,即使與用戶名無關。

要解決此問題,您必須同時加入日期和用戶名,例如,

LEFT JOIN sprint AS metric ON weight.weighInDate = metric.workoutDate 
           and weight.username = metric.username 

同樣在右側加入的,當然。


添加到答案,因爲它有助於說明問題的原因。 @OlafDietsche有正確的解決方案。我只是想讓你看看實際的錯誤數據來自哪裏。

爲了顯示你與你的加入此記錄的問題:

| testuser | 2016-10-17 |   3.40 

也加入到這個紀錄

| jdoe  | 2016-10-17 | 160 | 

因爲你只上加入日期。