faqts : Computers : Databases : MySQL : Language and Syntax : Queries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

37 of 50 people (74%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

how do i select only unique values in a column?

Jul 31st, 2000 13:00
John Marc, deno vichas,


Suppose you had a table with fields custid,due,state
Now, you want a unique list of states, but also the TOTAL amount due 
from each state.
SELECT sum(due), state FROM table GROUP BY STATE.
The group by is what you are looking for.  
Note, you can use more than one field as in:
GROUP BY city,state 
to widen the uniqueness to the city level, but avoid lumping Paris, 
texas with Paris, NY