Reputation: 646
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
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
Reputation: 72991
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