1

我有一个包含以下数据的表格:

+----+-----------------+
| id | country         |
+----+-----------------+
|  1 | i'm from usa    |
|  2 | i'm from italy  |
|  3 | i'm from china  |
|  4 | i'm from india  |
|  5 | she's from usa  |
|  6 | he's from china |
+----+-----------------+

我想通过检查列中的国家名称来了解每个国家的人口country。我想要这样的东西:

+---------+------------+
| country | population |
+---------+------------+
| usa     | 2          |
| italy   | 1          |
| china   | 2          |
| india   | 1          |
+---------+------------+

我想我应该使用GROUP BYCOUNT()功能。但是如何?谢谢。

4

5 回答 5

8

如果国家总是在最后你可以用这个。

select
  case 
    when country like '%usa' then 'usa'
    when country like '%italy' then 'italy'
    when country like '%china' then 'china'
    when country like '%india' then 'india'
  end as ccountry,
  count(*) as population
from Table1
group by ccountry;

如果 country 可以在字符串中的任何位置,您可以像这样找到它,假设它位于开头、结尾或中间,由 . 包围space

select
  case 
    when country like '% usa %' then 'usa'
    when country like '% italy %' then 'italy'
    when country like '% china %' then 'china'
    when country like '% india %' then 'india'
  end as ccountry,
  count(*) as population
from 
    (
      select concat(' ', country, ' ') as country
      from Table1
    ) T
group by ccountry
于 2012-04-14T21:18:33.180 回答
4

假设国家名称始终是最后一个组件country(组件由空格分隔),那么您可以这样做:

select substring_index(country, ' ', -1) as real_country, count(*)
from your_table
group by real_country

给你国家的substring_index(country, ' ', -1)最后一个“词”。

于 2012-04-14T21:20:51.633 回答
1

未经测试,但可能是解决方案

select SUBSTRING(country,(INSTR(country,'from') +5)), count(1) 
from table group by SUBSTRING(country,(INSTR(country,'from') +5))
于 2012-04-14T21:08:13.947 回答
0

也许这有效:

SELECT PARSENAME(REPLACE(country, ' ', '.'), 1) as parsedCountry, count(*) AS population
FROM table
GROUP BY parsedCountry

解释: 1. REPLACE(country, ' ', '.') 只是用一个点替换所有出现的空格。所以“she's from usa”将是“she's.from.usa”

2. PARSENAME("she's.from.usa", 1) 将在点上分割字符串。然后从后到前数1,得到那部分字符串。将返回“美国”

3. FROM table 我不知道你的桌子叫什么名字……所以我放了桌子。

4. GROUP BY parsedCountry 它将对 after-parsename-replace-country 的出现进行分组。

于 2012-04-14T21:21:51.217 回答
0

如果您的“国家/地区”名称可以来自另一个表,则可以选择以下选项。这可以随着您的“国家”名称列表的增长而灵活增长,而无需进入和编辑 SQL 语句。

我创建了一个临时表 #citizens 来匹配您的示例输入:

create table #citizens (id int, country varchar(30) )

insert into #citizens (id, country) values (1, 'i''m from usa')
insert into #citizens (id, country) values (2, 'i''m from italy')
insert into #citizens (id, country) values (3, 'i''m from china')
insert into #citizens (id, country) values (4, 'i''m from india')
insert into #citizens (id, country) values (5, 'she''s from usa')
insert into #citizens (id, country) values (6, 'he''s from china')

然后我创建了一个临时表#countries 来保存选择的国家名称

create table #countries (country varchar(30) )

insert into #countries values('usa')
insert into #countries values('china')
insert into #countries values('india')
insert into #countries values('italy')

所需的选择看起来像这样。注意类似 '%' ...

select co.country, COUNT(*) 
from #countries co
left outer join #citizens ci on ci.country like '%'+co.country+'%'
group by co.country

因为我只是在玩,后来我把临时桌子放下了。

drop table #countries
drop table #citizens
于 2012-04-14T21:36:49.140 回答