在MySQL中返回重复的记录

时间:2013-05-30 19:54:22

标签: mysql 4d-database

不幸的是,在我职业生涯的大部分时间里,我使用了一个名为4th dimension的怪异法语数据库。它与MySQL的行为截然不同。所以请关注我可能非常简单的问题。

我有一个查询如下:

SELECT c.id, p.last_name, e.full_name, l.Current_step, l.Current_Step_date,  
FROM customer as c, lesson as l , Employee as e 
WHERE l.Prospect_ID =c.id
AND c.ID_ofProducer= e.id  
AND last_name = 'Smith' 

我正在收回3条记录。但它们都是一样的。

ID      Last_name     Full_name     Current_Step      Current_Step_date
61245   Smith         Jim Jones     Registered        2013-04-14
61245   Smith         Jim Jones     Registered        2013-04-14
61245   Smith         Jim Jones     Registered        2013-04-14

我不希望得到61245 Smith

但;我没想到会回复3次。

有人可能会对我做错了什么/我应该做什么有所了解?

2 个答案:

答案 0 :(得分:1)

我认为你可以使用GROUP BY聚合函数。

SELECT c.id, p.last_name, e.full_name, l.Current_step, l.Current_Step_date,  
FROM customer as c, lesson as l , Employee as e 
WHERE l.Prospect_ID =c.id
AND c.ID_ofProducer= e.id  
AND last_name = 'Smith'   
GROUP BY c.id

答案 1 :(得分:1)

其他领域可能存在差异。要看到它们,这应该有效:

SELECT * 
FROM customer as c, lesson as l , Employee as e 
WHERE l.Prospect_ID =c.id
AND c.ID_ofProducer= e.id  
AND last_name = 'Smith' 

如果没有差异,则一个或多个表中存在重复。

如果您想消除此类重复项,可以使用distinct关键字执行此操作:

SELECT distinct c.id, p.last_name, e.full_name, l.Current_step, l.Current_Step_date
FROM customer as c, lesson as l , Employee as e 
WHERE l.Prospect_ID =c.id
AND c.ID_ofProducer= e.id  
AND last_name = 'Smith' 

如果不支持,您可以使用group by执行相同操作:

SELECT c.id, p.last_name, e.full_name, l.Current_step, l.Current_Step_date
FROM customer as c, lesson as l , Employee as e 
WHERE l.Prospect_ID =c.id
AND c.ID_ofProducer= e.id  
AND last_name = 'Smith' 
group by c.id, p.last_name, e.full_name, l.Current_step, l.Current_Step_date