2012-07-19 115 views
0

我有兩個屬性和圖像的表,它們具有相同的列名,如id,兩個表中的名稱。父列是圖像中的外鍵。加入mysql需要爲每個主鍵返回單個記錄

SELECT DISTINCT(A.id),A.name,B.name AS img 
FROM `jos_properties_products` AS A 
LEFT JOIN `jos_properties_images` AS B ON A.id = B.parent 

enter image description here

從上面我想刪除重複的。

+0

你的意思是刪除重名? – sel 2012-07-19 06:13:20

+0

是的。我想刪除重複 – muthu 2012-07-19 06:16:36

+1

添加在「GROUP BY名稱」 – sel 2012-07-19 06:19:06

回答

1

嘗試:

SELECT A.id,A.name,max(B.name) AS img 
FROM `jos_properties_products` AS A 
LEFT JOIN `jos_properties_images` AS B ON A.id = B.parent 
group by A.id,A.name 
+0

我知道爲什麼我們必須使用max()嗎? – muthu 2012-07-19 06:17:19

+0

您有多個B.name與單個id-name組合關聯。這裏你想避免id-name重複。因此,對於每個id-name取B.name的最大值。你可以使用min()而不是max() – 2012-07-19 06:32:04

0

SELECT DISTINCT(A.id)如IDS,A.name aName,B.name AS IMG FROM jos_properties_products AS甲 LEFT JOIN jos_properties_images AS B開A.id = B.parent group by ids,aName,img;

我認爲這會做

相關問題