【问题标题】:MySQL Comparison QueryMySQL比较查询
【发布时间】:2015-06-09 06:31:03
【问题描述】:

我需要比较所有区域并显示分数更好的区域。例如,如果我想查看亚洲的得分比较;我只会看到分数更好的区域。 (较好的地区:北美、欧洲)

select
    Destination_Region,
    count(1) as Score
from pro11
    where 
        Destination_Region is not null
    group by 
        Destination_Region 
    order by 
        Score DESC;

查询结果:

Europe             1069737
North America      218302
Asia               140452
Middle East        70899
Africa             33901
Oceania            28701
South America      24815
Caribbeans         16550

所以如果我想看看非洲的比较结果;我应该去中东、亚洲、北美、欧洲。我将使查询动态化,您可以选择一个区域并使用它。

谢谢!

【问题讨论】:

  • 您有问题吗?
  • 是的,我不知道如何使用 Mysql。
  • 我认为您可以使用自连接来实现这一点,将尽快发布查询。

标签: mysql comparison


【解决方案1】:

使用HAVING 子句检查:

select
    Destination_Region,
    count(*) as Score
from pro11
    where 
        Destination_Region is not null
    group by 
        Destination_Region 
    having  Score>(SELECT COUNT(*) FROM pro11 WHERE Destination_Region = 'Africa' GROUP BY Destination_Region)
    order by Score DESC;

SQL Fiddle 中的示例结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    相关资源
    最近更新 更多