簡體   English   中英

MySQL比較查詢

[英]MySQL Comparison Query

我需要比較所有區域,並顯示得分更高的區域。 例如,如果我想查看亞洲的得分比較; 我只會看到比分更好的區域。 (更好的地區:北美,歐洲)

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

因此,如果我想看看非洲的比較結果; 我應該看到中東,亞洲,北美,歐洲。 我將使查詢動態化,您可以選擇一個區域並使用它。

謝謝!

使用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中的示例結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM