加入mysql需要为每个主键返回单个记录

时间:2012-07-19 06:05:13

标签: mysql

我有两个属性和图像表,它们具有相同的列名,如两个表中的id,name。父列是图像中的外键。

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

从上面我想删除重复。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

SELECT DISTINCT(A.id)as ids,A.name aName,B.name AS img 来自jos_properties_products AS A LEFT JOIN jos_properties_images AS B ON A.id = B.parent group by ids,aName,img;

我认为这样做