mysql - 拥有 2 种或更多官方语言且非官方语言多 2 倍的国家/地区列表
问题描述
我有下一个模型的任务:
接下来的任务是:输出具有两种或两种以上官方语言的国家列表,其中非官方语言的数量至少是官方语言的两倍。
这个任务看起来很简单,但我有一个错误。我接下来尝试了:
SELECT name, countrylanguage.*
FROM country, countrylanguage
WHERE countrycode = code AND isofficial LIKE "t"
GROUP BY isofficial
HAVING COUNT(*) => 2;
但我只有一个条目回来。
列出所有拥有超过 2 种官方语言的国家:
SELECT country.name
FROM country
JOIN countrylanguage
ON countrycode = code
WHERE isofficial = 't'
GROUP BY country.name
HAVING COUNT(*) >= 2;
但是我应该如何只选择非官方语言多两倍的国家呢?
解决方案
内部查询似乎是最简单的方法。从至少有两种官方语言的国家列表开始:
select countrycode, count(1)
from countrylangauge
where isofficial
group by countrycode
having count(1) >= 2
加入类似的查询可以获取非官方语言计数
select official.countrycode,
official.number 'Official Langs',
unofficual.number 'Unofficual Langs'
from (select countrycode, count(1) as number
from countrylangauge
where isofficial
group by countrycode
having count(1) >= 2
) offical
inner join (select countrycode, count(1) as number
from countrylangauge
where not isofficial
group by countrycode
-- Since min count of offical is 2, and need double unoffical
-- then need at lease 4 here
having count(1) >= 4
) unoffical
on unofficual.countrycode = offical.countrycode
and unofficual.number >= 2*officual.countrycode
随时加入以country
获取有关国家/地区的更多详细信息。
推荐阅读
- python - 使用 Python 将 ini 文件中的所有内容读入有序字典
- php - 在 Woocommerce 3 中随处替换产品价格的自定义字段值
- wpf - 如果您要绑定到具有只读属性的对象,是否仍应实现 INotifyPropertyChanged?
- android - 当超过 20 个 Fragment 添加到 backstack 时,应用程序变慢
- angular - angular6 和 angular5 中 prepareExternalUrl 的不同行为
- java - 无法使用新版本的 android studio 模拟器运行 android 项目
- angular - 从数组构建表单控件 - Angular 4
- python - 调用函数时的 Jupyter Notebook NameError
- angular - 如何在角度中使用单个布尔变量并在不同字段中动态使用
- javascript - React Native 中获取后的条件渲染