简体   繁体   English

MySQL-选择具有该不同计数的同一个DISTINCT

[英]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 ) 表格很简单,只是(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 20000
langb | langb | 10000 10000
langc | langc | 4000 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: 在这种情况下,您可以使用COUNT函数:

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

Use count() . 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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