Solved

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

Posted on 2009-04-13
5
626 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 150 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 350 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

707 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