我有兩個屬性和圖像的表,它們具有相同的列名,如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
從上面我想刪除重複的。
我有兩個屬性和圖像的表,它們具有相同的列名,如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
從上面我想刪除重複的。
嘗試:
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
我知道爲什麼我們必須使用max()嗎? – muthu 2012-07-19 06:17:19
您有多個B.name與單個id-name組合關聯。這裏你想避免id-name重複。因此,對於每個id-name取B.name的最大值。你可以使用min()而不是max() – 2012-07-19 06:32:04
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;
我認爲這會做
你的意思是刪除重名? – sel 2012-07-19 06:13:20
是的。我想刪除重複 – muthu 2012-07-19 06:16:36
添加在「GROUP BY名稱」 – sel 2012-07-19 06:19:06