MySQL加入错误的结果

时间:2015-06-26 14:26:14

标签: mysql left-join

我有一个关于JOIN的快速问题。 这是我的疑问:

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

显示相同的blob。

这是我的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 |
+----+-----------+----+-----------------------------------------------+

从图片中可以看出,这是错误的,因为imagedata中的ID不同。

1 个答案:

答案 0 :(得分:1)

您有2行具有相同的值:

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

因此,如果price_history表中的行包含此类数据:

imagename_dioPlus imagename_inkDepot
2                 3

将输出

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

无论他们有不同的ids。麻烦的是他们有相同的描述。

豫ICP备18024241号-1