1

If I have a table like so:

  • Apple
  • Orange
  • Pear
  • Pear
  • Orange

How do I view how many different words there are? In this example it would be three:

  1. Apple
  2. Orange
  3. Pear
fusion3k
  • 11,568
  • 4
  • 25
  • 47
Ideen
  • 47
  • 1
  • 7

2 Answers2

3
SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name
Severino Lorilla Jr.
  • 1,637
  • 4
  • 20
  • 33
  • If I had "Apples, Oranges" in one row. Would it be possible to split the fruits into two between the commas? – Ideen Apr 06 '16 at 00:38
  • well, you can't do that with the query above as it will look for the whole string "Apples, Oranges". Maybe you can do some pre-processing before running the query? – Severino Lorilla Jr. Apr 06 '16 at 00:47
1
  1. To get the no. of diff words you can use either group by or distinct. select words_column_name, count(words_column_name) as no_of_repetation from table_name group by words_column_name. or just select distinct(words_column_name) from table_name

2.To split the word, there is no in build function, but you can do it using substring() & locate(). like:

SUBSTRING_INDEX(SUBSTRING_INDEX(fullword, ',', 1), ',', -1) AS first_fruit,
    TRIM( SUBSTR(fullname, LOCATE(',', fullword)) ) AS last_fruit

But this is applicable only for 2 fruit combination, for more you need to write some php script or mysql function

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14