Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL / SQL syntax - using DISTINCT - not working as expected.

Posted on 2009-04-13
5
Medium Priority
?
629 Views
Last Modified: 2012-05-06
Hi,

I'm sure I have acheived this before but seem to be having problems recreating.

I want to display a list of "suppliers". A supplier can have more then one email address and email addresses are stored in a different table (email__addresses).

I want to display a list of suppliers on a page and I want that list of suppliers to display an just one email address for each supplier (does not matter which, just display one).

Of course when I do the query using a LEFT JOIN to link the tables it gives me my results, but it a supplier has more then one email address it is listed more then once.

Now I thought I had come across this problem before and simply used the
SELECT DISTINCT(suppliers.supplier_id) to make sure that at most each supplier isonly displayed once.

but when I use the code as shown in the code window it still displays a supplier multiple times if that supplier has more then one email address.

What am I missing?
Thanks


SELECT DISTINCT(suppliers.supplier_id), suppliers.*, email_addresses.email_address  FROM suppliers 
LEFT JOIN email_addresses ON (supplier_id = emails_supplierid )

Open in new window

0
Comment
Question by:afflik1923
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Assisted Solution

by:dportas
dportas earned 600 total points
ID: 24131047
DISTINCT returns unique ROWS. DISTINCT will not pick one row from the joined table, it just eliminates duplicate rows from the end result. The brackets in your query aren't needed and do nothing.

To help us answer your question with a new query please tell us what is/are the key(s) of the email_addresses table?
0
 
LVL 14

Accepted Solution

by:
racek earned 1400 total points
ID: 24131048
SELECT s.supplier_id, s.xx, s.yyy,
            MIN(ea.email_address) email_address
 FROM suppliers s
LEFT JOIN email_addresses ea
  ON  supplier_id = emails_supplierid
GROUP BY s.supplier_id;
0
 
LVL 14

Expert Comment

by:racek
ID: 24131065
SELECT s.*, x.email_address
 FROM suppliers s
LEFT JOIN
   (SELECT emails_supplierid, MIN(email_address) email_address
     FROM  email_addresses GROUP BY 1 ) as  x
  ON  s.supplier_id = x.emails_supplierid ;
0
 

Author Comment

by:afflik1923
ID: 24131134
Ahh. I think I was getting myself in a muddle. Seeing the code there I think it was GROUP BY I also used. I will try and report back.
Thanks
0
 

Author Closing Comment

by:afflik1923
ID: 31569577
Yes of course. It was the group by Supplier_id that done the trick. I was being a bit silly there.

Thanks
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question