6
CREATE TABLE Countries(location varchar(255), country varchar(255))

INSERT INTO Countries(location, country)
               VALUES('Arkansas', 'US'),
                      ('Newyork', 'US'),
                      ('New Jersey', 'US'),
                      ('Tokyo', 'JP'),
                      ('Yokohama', 'JP'),
                      ('Chennai', 'IN'),
                      ('Delhi', 'IN'),
                      ('Sydney', 'AU'),
                      ('Melbourne', 'AU');

我需要查询以下输出

 Location     |         Country
--------------------------------
  Arkansas                US
  Tokyo                   JP
  Chennai                 IN
  Sydney                  AU
  Newyork                 US
  Yokohama                JP
  Delhi                   IN
  Melbourne               AU
  New Jersey              US 
4

6 回答 6

4

您需要根据每个位置在其所在国家/地区内的相对顺序给每个位置一个排名。您可以使用变量在 MySQL 中创建临时行号函数:

SELECT  Country,
        Location,
        @r:= CASE WHEN Country = @c THEN @r + 1 ELSE 1 END AS RN,
        @c:= Country AS C2
FROM    Countries,
        (SELECT @r:= 1) r,
        (SELECT @c:= '') c
ORDER BY Country, Location;

这将输出

COUNTRY     LOCATION    RN  C2
AU          Melbourne   1   AU
AU          Sydney      2   AU
IN          Chennai     1   IN
IN          Delhi       2   IN
JP          Tokyo       1   JP
JP          Yokohama    2   JP
US          Arkansas    1   US
US          New Jersey  2   US
US          Newyork     3   US

然后你可以通过 RN 和 Country 订购这个以获得你想要的订单

SELECT  Location, Country
FROM    (   SELECT  Country,
                    Location,
                    @r:= CASE WHEN Country = @c THEN @r + 1 ELSE 1 END AS RN,
                    @c:= Country AS C2
            FROM    Countries,
                    (SELECT @r:= 1) r,
                    (SELECT @c:= '') c
            ORDER BY Country, Location
        ) c
ORDER BY rn, Country DESC;

SQL Fiddle 示例

编辑

由于您遇到排序规则错误,但没有指定排序规则错误是我希望纠正的唯一方法是对所有内容使用显式排序规则:

SELECT  Location, Country
FROM    (   SELECT  Country COLLATE utf8_general_ci AS Country,
                    Location COLLATE utf8_general_ci AS Location,
                    @r:= CASE WHEN Country = @c THEN @r + 1 ELSE 1 END AS RN,
                    @c:= Country COLLATE utf8_general_ci AS C2
            FROM    Countries,
                    (SELECT @r:= 1) r,
                    (SELECT @c:= '' COLLATE utf8_general_ci) c
            ORDER BY Country, Location
        ) c
ORDER BY rn, Country DESC

SQL 小提琴

于 2012-11-22T09:10:35.290 回答
1

试试这个:

在 mysql 中按 Field()使用order

select Location, Country
from Countries
order by Field(Location,'Arkansas','Tokyo','Chennai',...)
于 2012-11-22T09:04:23.623 回答
1

您可以使用 oracle 分析函数 rank() 执行此查询

这是相同的工作查询

select tbl.l,
tbl.c
from
(
select location l,
country c,
rank() over (partition by country order by rowid) rnk
from countries
order by rowid,rnk) tbl
order by rnk,rowid;
于 2012-11-25T15:59:48.397 回答
1

如果没有id. 你可以这样创建你的表:

CREATE TABLE Countries(
  id INT NOT NULL AUTO_INCREMENT,
  location varchar(255),
  country varchar(255),
  PRIMARY KEY(ID))

ant 然后你可以插入你的数据:

INSERT INTO Countries(location, country)
           VALUES('Arkansas', 'US'),
           ...

Ant 然后我将使用标准 SQL 编写查询,如下所示:

SELECT *
FROM Countries c1
ORDER BY (select count(*)
          from countries c2
          where c1.country=c2.country
          and c1.id>c2.id), id

这个查询可能不是很快,但它会起作用。但是如果不使用,id就无法回答您的问题。SQL 表没有默认顺序,因此如果没有,id则无法判断,例如,悉尼排在墨尔本之前,即使它是先插入的。

于 2012-11-26T19:46:23.377 回答
0
选择位置,国家
  FROM(选择国家、地点、
                  @r:= CASE WHEN Country = @c THEN @r + 1 ELSE 1 END AS RN,
                  @c:= 国家 AS C2
            从国家,
                  (SELECT @r:= 1) r,
                  (SELECT @c:= '') c
        )C
 ORDER BY rn,国家 DESC;

子查询中不应该有 Order by

于 2012-11-25T17:56:11.913 回答
0

在此处输入图像描述

为什么会这样。

它显示 MySQL Database Error: Illegal mix of collat​​ions (latin1_general_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='

于 2012-11-26T04:33:44.723 回答