【问题标题】:MySQL - assort and count itemsMySQL - 分类和计数项目
【发布时间】:2015-01-19 12:41:52
【问题描述】:

我的表有三列“名称”、“城市”和“国家”。 现在我想只列出那些至少有 3 倍相同城市的国家/地区。

 name        city        country
 ----        ----        -------
 Smith       Boston      USA
 Wayne       St. Louis   USA
 Miller      Houston     USA
 Joseph      Houston     USA
 Obama       Washington  USA
 Jones       Houston     USA

 Sarkozy     Paris       France
 Merle       Paris       France
 Gabin       Marseille   France
 Delon       Avignon     France
 Deneuve     Avignon     France

 Trappatoni  Rome        Italy
 Linguini    Milano      Italy
 Mastroianni Rome        Italy

 Meier       Hamburg     Germany
 Müller      Munich      Germany
 Schmidt     Hamburg     Germany
 Böttcher    Hamburg     Germany
 Kunau       Hannover    Germany
 Wilhelm     Munich      Germany
 -------------------------------

                         USA
 Result:             
                         Germany

我尝试了 distinct、count、group by 等。但没有结果。

【问题讨论】:

  • 你好 Jürgen,Wahnsinn,es hat geklappt!!!!!!!!!!!! Tausend Dank :-)

标签: mysql sql


【解决方案1】:

按国家和城市分组,然后只选择每个至少有 3 个条目的那些。

select distinct country
from your_table
group by country, city
having count(*) >= 3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多