FarwallGhost
FarwallGhost

Reputation: 718

Count the different strings of one column in mysql

I have a situation where I have to count number of strings of different type in one column of a table, e.g. a column would have values such as :

apple
apple
apple
orange
orange
banana
banana
banana
banana

So I need to count the strings only of different type, that means query should give count result 3. What can be the ideal query for this?

Upvotes: 0

Views: 318

Answers (4)

Aleks G
Aleks G

Reputation: 57316

Instead of using simple COUNT(field), you could use COUNT(DISTINCT field):

SELECT COUNT(DISTINCT fieldname) FROM tablename

If your values can have different upper/lower case variants and you still only want the real unique (e.g. Banana is the same as banana), then you can add lower function into the mix:

SELECT COUNT(DISTINCT lower(fieldname)) FROM tablename

Upvotes: 0

nes
nes

Reputation: 1096

You can count distinct values like this:

SELECT COUNT(DISTINCT column_name) FROM table_name;

Upvotes: 2

malta
malta

Reputation: 858

Try this:

SELECT COUNT(DISTINCT colmnname) FROM tablename

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

So use COUNT with DISTINCT of string field stringField in your table t:

SELECT COUNT(DISTINCT stringField) FROM t

Upvotes: 2

Related Questions