我有两个属性和图像的表,它们具有相同的列名,如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