Welcome to the OzoneAsylum FaqWiki
Frequently Asked Questions
Server Side Coding
MySQL

Why doesn't DISTINCT help trim down the results returned from my MySQL SELECT statement? Pages that link to <a href="https://ozoneasylum.com/backlink?for=5699" title="Pages that link to Why doesn&amp;#039;t DISTINCT help trim down the results returned from my MySQL SELECT statement?" rel="nofollow" >Why doesn&#039;t DISTINCT help trim down the results returned from my MySQL SELECT statement?\

DISTINCT is designed to cut out the repetition of identical rows not just ones that might share the same ID or name.

To trim down your results where DISTINCT isn't helping try using the GROUP BY attribute

-------------------
Basicly, there's probably something wrong with your query, if you feel the need to use DISTINCT. The number of times I *really* needed to use (the mathematically more correct) DISTINCT, I can count on the fingers of one hand of a guy who worked 40 years in a sawmill...
So think about what joins you're doing, how you're filtering and why you're getting 'to many rows' (which can not be fixed by distinct!)

(Math note: In set algebra, there is only a 'Select Distinct' since sets by their very definition contain no duplicates. Why the SQL creators went with bags and 'Select All' by default... I don't know. Therefore, 'Select Distinct' only eliminates real duplicates from your result set. )

----------------------------
Relevant links:
Mysql Documentation:
6.4.1 SELECT Syntax
3.3.4.3 Selecting Particular Columns
6.3.7 Functions for Use with GROUP BY Clauses
5.2.5 How MySQL Optimises DISTINCT

Outside Resources:
Select Distinct Is a Code Smell (very informative!)
Always Use Select Distinct

_____________________
Emperor

(Edited by: Tyberius Prime on Sat 24-May-2003)

« BackwardsOnwards »

Show Forum Drop Down Menu