PDA

View Full Version : Exclude Duplicates


RobP
3-28-05, 02:04 AM
I am pretty new to MySQL and hopefully have an easy question.

I have a database that I am attempting to work with. I am attempting to pull the Customer Numbers for all Customers who would happen to be within an Zipcode. The problem I am having is that the Customers may have more then on address in the same Zipcode within my Address table. Which causes duplicate entries in my results. All address are linked by the Customer's unique number. An example would be

CustomerNumber Address Zip
1 123 AnyStreet 12345
1 321 Another St. 12345
2 567 SQL Ave 34531
3 851 Some St 12345

If I do a query (SELECT CustomerNumber FROM Addresses WHERE Zip=12345) looking for Customer Numbers where Zip=12345 I get 3 results returned. (1,1,3 for the 1st, 2nd and 4th) What I would like to happen is to get just 2 results returned (1,3) that would tell me that Customer Number 1 and 3 are within the 12345 Zipcode. I could care less on this search that Customer #1 has 2 locations within the same Zipcode.

So far the only way I can think of doing this is to take my results and then run several loops through the Array of results using PHP to only grab the Customer Number 1 time. I am hoping that there may be an easier way.

Any Help would be great!

RobP
3-28-05, 03:30 AM
I Finally figured out the answer to this question. Turns up while I was reading prior posts to other issues, I found someone who was getting the result I wanted (But they did not want) by using "GROUP BY" Tried it out on phpmyAdmin and it worked like a champ!

Changed the Statement to: SELECT CustomerNumber FROM Addresses WHERE Zip=12345 GROUP BY CustomerNumber

Caligatio
3-29-05, 12:34 AM
It looks like you found the solution to your problem, but http://www.w3schools.com/sql/ has EXCELLENT information on queries.