Solved

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

Posted on 2009-04-13
5
624 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
  • 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

792 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