我正在尋找解決方案,因爲小時,但貓得到它。SQL自加入沒有雙交叉結果
我的表:
ID name value1 value2
1 Meyer 20 _
2 Schmitt 20 _
3 Berger _ 20
4 Chief _ 20
我的查詢:
SELECT A.ID, A.name AS nameA, B.name AS nameB, A.value1
FROM table AS A INNER JOIN table AS B
ON A.value1 = B.value2
結果:
ID nameA nameB value1
1 Meyer Berger 20
1 Meyer Chief 20
2 Schmitt Berger 20
2 Schmitt Chief 20
這應該是這樣的:
ID nameA nameB value1
1 Meyer Berger 20
2 Schmitt Chief 20
我怎樣才能得到這個結果?
我也嘗試了GROUP BY A.name,但得到錯誤的結果:
ID nameA nameB value1
1 Meyer Berger 20
2 Schmitt Berger 20
究竟是什麼你想在這裏做什麼?你應該閱讀數據庫規範化。 – fluffy
我不理解你如何關聯記錄。如果「value1」和「value2」的所有值都是20,那麼你如何確定「Meyer」只屬於「Berger」,而「Schmitt」只屬於「Chief」? –
您將需要一些其他條件來完成此項工作。例如,爲什麼查詢只返回Meyer Berger而不是Meyer Chief? – Phil