MySQL - 按字母顺序选择首先出现的名称

时间:2015-05-14 07:41:21

标签: mysql sql

我已经开始学习MySQL了。

这是表格world

+-------------+-----------+---------+
|    name     | continent |  area   |
+-------------+-----------+---------+
| Afghanistan | Asia      | 652230  |
| Albania     | Europe    | 2831741 |
| Algeria     | Africa    | 28748   |
| ...         | ...       | ...     |
+-------------+-----------+---------+

我需要:

  

列出每个大洲以及按字母顺序排列的国家/地区名称

SELECT的结果必须是:

+---------------+---------------------+
|   continent   |         name        |
+---------------+---------------------+
| Africa        | Algeria             |
| Asia          | Afghanistan         |
| Caribbean     | Antigua and Barbuda |
| Eurasia       | Armenia             |
| Europe        | Albania             |
| North America | Belize              |
| Oceania       | Australia           |
| South America | Argentina           |
+---------------+---------------------+

12 个答案:

答案 0 :(得分:23)

这是一个简单的aggegation:

SELECT continent, MIN(name) AS name
FROM world 
GROUP BY continent
ORDER by continent

答案 1 :(得分:11)

如果它是来自SQLZoo的练习,而不是IMO,它应该是这样的:

select continent, name from world x
where name = (select name 
                  from world y 
                  where  x.continent =  y.continent 
                  order by name asc 
                  limit 1)

P.S。我现在从那里学习SQL,这篇文章帮助了我。感谢@Parado!)

更新:我找到了这个site的答案。堆叠时很有用。

答案 2 :(得分:6)

试试这个

select distinct  w.continent, 
                 (select w2.name 
                  from world w2 
                  where  w.continent =  w2.continent 
                  order by name asc 
                  limit 1) name 
from world w
order by w.continent

答案 3 :(得分:1)

SqlZoo解决方案最好如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = { "key=value", "eureka.client.enabled=false" }
)
public class NewBootTest {

    @Value("${key}")
    public String key;

    @Test
    public void test() {
        System.out.println("great " + key);
    }
}

答案 4 :(得分:1)

SELECT  distinct x.continent , x.name
FROM world x ,world y 
WHERE x.name = (SELECT y.name FROM world y
 WHERE y.continent=x.continent order by y.name asc limit 1 ) ;

答案 5 :(得分:0)

这个sql怎么样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace most_frequent_int
{
class Program
{
    static void Main(string[] args)
    {
        string result = mostFreq(new int[] {1,5,2,5,24,6,5});
        Console.WriteLine(result);
    }

    static string mostFreq(int[] number)
    {
        int element = 0;
        int count = 0;

        for (int j = 0; j < number.Length; j++)
        {
            int tempElement = number[j];
            int tempCount = 0;

            for (int p = 0; p <number.Length; p++)
            {
                if (number[p] == tempElement)
                {
                    tempCount++;
                }
                if (tempCount > count)
                    element = tempElement;
                {
                    count = tempCount;
                }
            }
        }
return "The most frequent element is: " + element + " and appears " + count + " times."        
    }

}

答案 6 :(得分:0)

SELECT continent,
       name
FROM world x
WHERE name=
    (SELECT name
     FROM world y
     WHERE x.continent=y.continent
     ORDER BY name
     LIMIT 1)

这是相关/同步查询。

答案 7 :(得分:0)

select continent, name
from
(select continent, name, rank() over(partition by continent order by name) r1
from world) a
where a.r1 = 1

答案 8 :(得分:-1)

Select distinct continent, name from world x 
where name <=All (Select name from world y where x.continent=y.continent)

答案 9 :(得分:-2)

select continent, name from world group by continent order by name

答案 10 :(得分:-3)

试试这个

SELECT continent, name FROM world ORDER BY name ASC;

答案 11 :(得分:-3)

如果您需要按字母顺序列出每个大陆,请使用

     SELECT * from world ORDER by continent

但是,如果您列出了您使用过的每个国家/地区

     SELECT * from world ORDER by name