2015-06-26 69 views
0

我有一個關於JOIN的快速問題。 這裏是我的查詢:MySQL加入wong結果

SELECT 
products.name AS name, 
products.last_scanned AS last_scanned, 
products.last_scanned AS last_changed, 

ph.price AS price, 
ph.mirror_price AS mirror_price, 
ph.change_date AS change_date, 

ph.dioPlusZapPrice AS dioPlusZapPrice, 
ph.dioPlusSitePrice AS dioPlusSitePrice, 

ph.inkDepotZapPrice AS inkDepotZapPrice, 
ph.inkDepotSitePrice AS inkDepotSitePrice, 

ph.misradiaZapPrice AS misradiaZapPrice, 
ph.misradiaSitePrice AS misradiaSitePrice, 

ph.netPrintZapPrice AS netPrintZapPrice, 
ph.netPrintSitePrice AS netPrintSitePrice, 

img1.image_blob AS dioPlusImage, 
img2.image_blob AS inkDepotImage, 
img3.image_blob AS misradiaImage, 
img4.image_blob AS netPrintImage 

FROM 
price_history ph 

LEFT JOIN products ON products.productID=ph.productID 

LEFT JOIN imagedata img1 ON img1.id = ph.imagename_dioPlus 
LEFT JOIN imagedata img2 ON img2.id = ph.imagename_inkDepot 
LEFT JOIN imagedata img3 ON img3.id = ph.imagename_misradia 
LEFT JOIN imagedata img4 ON img4.id = ph.imagename_netPrint 

ORDER BY ph.change_date DESC 

此查詢的結果是:

Brother MFC-L2700DW|2015-06-26 15:47|2015-06-26 15:47|949|949|2015-06-26 15:47|949|949|969|999|0|0|0|0|[BLOB - 217.9 KiB]|[BLOB - 217.9 KiB]|NULL|NULL 

這顯示相同的斑點。

這是我imagadata表:

+----+-----------+------------+---------------------------------------+ 
| ID | image_blob   | ID | imagename       | 
+----+-----------+-----+----------------------------------------------+ 
| 1 | [BLOB - 19.5 KiB] | 1 | a5052c6e-1120-4ece-994d-2cdac7e7baa4 | 
| 2 | [BLOB - 217.9 KiB] | 2 | 1ed6f935-a44a-4f07-9c6b-fa51da5d4bdb | 
| 3 | [BLOB - 217.9 KiB] | 3 | 8acced70-17ae-4a65-8466-6b910c674869 | 
+----+-----------+----+-----------------------------------------------+ 

正如你可以從圖片看,這是錯誤的,因爲該ID是imagedata不同。 任何想法如何解決這個問題?

回答

1

你有2行具有相同值:

2 - [BLOB - 217.9 KiB]; 
3 - [BLOB - 217.9 KiB]; 

所以,如果行price_history表containt這樣的數據:

imagename_dioPlus imagename_inkDepot 
2     3 

它將輸出

[BLOB - 217.9 KiB] [BLOB - 217.9 KiB] 

不管他們有不同的ids。麻煩的是他們有相同的描述。