我有一個表Mysql的自聯接數據
distributor_id | product_id | price
1 | 1 | 10.00
1 | 2 | 11.00
1 | 3 | 12.00
1 | 1 | 14.00
4 | 1 | 9.00
4 | 2 | 32.00
4 | 5 | 17.00
我想比較的分銷商的產品價格,即我想得到這樣的輸出:
distributor1|distributor2|product_id|distributor1_price|distributor2_price
1 | 4 | 1 |12.00 |9.00
1 | 4 | 2 |11.00 |32.00
1 | 4 | 3 |12.00 |null
distributor1_price,distributor2_price將均價由product_id。 product_id應該是分銷商的所有產品1。如果分銷商2沒有該產品,則應該爲空。
我已經嘗試過與自我連接沒有任何成功。 謝謝。
你沒有PRIMARY KEY。目前,這個問題是不可解的 - 除非第4行是錯誤的 – Strawberry