Skizz
Skizz

Reputation: 646

SQL: List unique substring from fields in table

I'm running a query to retrieve the first word from a string in a colum like so..

SELECT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`

But this allows duplicates, what do I need to add to the statement to get unique list of words out?

Upvotes: 0

Views: 501

Answers (2)

Cameron S
Cameron S

Reputation: 2301

To remove duplicates in a SELECT statement, change to SELECT DISTINCT column. In this case:

SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`

Upvotes: 1

Jason McCreary
Jason McCreary

Reputation: 72991

DISTINCT

For example:

SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word` FROM `your_table`

Note: There are performance implications for DISTINCT. However, in your case, there is likely limited pure MySQL alternatives.

Upvotes: 1

Related Questions