Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now