Question

Unfortunately for most of my career I use a freaky french database called 4th dimension. And it acted very differently from MySQL. So please bear with me on what could be avery simple question.

I have bult a query as follows:

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' 

I am getting back 3 records. But they are all the same.

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

I DID expect to get 61245 Smith back.

But; I did not expect to get it back echoed 3 times.

Can someone possibly shed some light on what the heck I did wrong/ what I should have done?

Was it helpful?

Solution

There are probably differences in other fields. To see them, this should work:

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' 

If there are no differences, then there are duplicates in one or more of the tables.

If you want to eliminate such duplicates, you can do so with the distinct keyword:

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' 

If that is not supported, you can do the same thing with 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

OTHER TIPS

I think you can use GROUP BY aggregate function.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top