Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT DISTINCT and count

the table looks like this:

tag | entryID
----+---------
foo | 1
foo | 2
bar | 3

And now i want to get all tags with its usage:

foo | 2
bar | 1

How can I do this?

Thank you

like image 944
jake Avatar asked Aug 15 '10 23:08

jake


People also ask

How do I select distinct and count in SQL?

The correct syntax for using COUNT(DISTINCT) is: SELECT COUNT(DISTINCT Column1) FROM Table; The distinct count will be based off the column in parenthesis. The result set should only be one row, an integer/number of the column you're counting distinct values of.

Can you use distinct and count together?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table.

Can you count distinct in SQL?

To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the COUNT function as DISTINCT . When given a column, COUNT returns the number of values in that column. Combining this with DISTINCT returns only the number of unique (and non-NULL) values.

What is count and count distinct?

Count would show a result of all records while count distinct will result in showing only distinct count. For instance, a table has 5 records as a,a,b,b,c then Count is 5 while Count distinct is 3.


1 Answers

select tag, count(*)
from MyTable
group by tag
like image 70
D'Arcy Rittich Avatar answered Nov 16 '22 02:11

D'Arcy Rittich