我已經使用合併來產生結果,但獲得了具有空值的重複行。避免合併中的空值
示例表如下:
Table - Student
ID Student Subject id Subject Grade id
100 Peter 200 Chinese 201
101 Mary 300 English 302
102 Sam 400 Maths 403
103 John 900 Music
Chinese Table
Subject id Grade id Grade
200 201 Good
200 202 Average
200 203 Poor
English Table
Subject id Grade id Grade
300 301 Good
300 302 Average
300 303 Poor
Maths Table
Subject id Grade id Grade
400 401 Good
400 402 Average
400 403 Poor
Select Id, Name,
Coalesce (chinese.grade, english.grade, maths.grade)
from Student
Left join Chinese On student.id = Chinese.subjectId AND student.gradeId = Chinese.gradeId
Left join English On student.id = English.subjectId AND student.gradeId = Enlgish.gradeId
Left join Maths On student.id = Maths.subjectId AND student.gradeId = Maths.gradeId
Result
ID Student Subject Grade
100 Peter Chinese Good
100 Peter Chinese NULL
101 Mary English Average
101 Mary English NULL
102 Sam Maths Poor
102 Sam Maths NULL
103 John Music NULL
我不知道在哪裏Null值的重複行了的,只是想知道我怎麼能避免使用合併給空值?
你應該向我們展示完整的select語句。有點難以告訴你什麼是錯誤的,當你不顯示損壞的部分。 – Hogan
嗨Hogan,如上所述的編輯表,你可以請檢查。 – GoodToBe1990
爲什麼你有三張不同的桌子?將所有類放在同一個表中。 – jarlh