简体   繁体   中英

MySQL - Select DISTINCT with Count Of That Distinct , Same Table

How do I turn the following query into the distinct result but with a count of the words returned with the same result? I could do a separate query, but there's a way to combine them.

table is simple, it's just ( id, info, language )

SELECT DISTINCT(`language`) FROM `info`

Easy enough, returns all of the languages that are unique. However, how do I count how many words have the language, and return within the same query.

Example of the results it would need to return

language | count of how many words are of this language
langa | 20000
langb | 10000
langc | 4000

You need to use grouping if you want to achieve the result with aggregating the data. You can use COUNT function in this case:

SELECT `language`, COUNT(`id`) FROM `words`
GROUP BY `language`

Use count() .

This will return the number of words per language

select language, count(word) as n_words
from tbl_word
group by language

If you want the number of unique words per language, then use this:

select language, count(distinct word) as n_unique_words
from tbl_word
group by language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM