MySQL - 条件MIN MAX返回不同的记录

问题描述 投票:0回答:3

我有来自the geonames website for Great Britain的数据库转储。它包含大约60000条记录。示例数据如下:

id       |     name    |   admin1   |   admin2   |  admin3  |  feature_class  |  feature_code
-------------------------------------------------------------------------------------------
2652355  |   Cornwall  |   ENG      |     C6     |          |      A          |    ADM2
11609029 |   Cornwall  |   ENG      |            |          |      L          |    RGN
6269131  |   England   |   ENG      |            |          |      A          |    ADM1

具有特征代码ADM2的第一条记录表示它是管理级别2具有特征代码RGN的secord记录表示它是一个区域。

我想按地名搜索记录以构建自动完成功能。如果记录具有相同的名称,并且如果其中一个记录是一个区域,即具有feature_code RGN,那么我只想返回该记录,否则我想返回与该ID最低的名称匹配的记录。

我尝试了以下但它不起作用:

   SELECT IF(t0.feature_code = 'RGN', MAX(t0.id), MIN(t0.id)) as id
       , CONCAT_WS(', ', t0.name,
                  IF(t3.name != t0.name, t3.name, NULL),
                  IF(t2.name != t0.name, t2.name, NULL),
                  IF(t1.name != t0.name, t1.name, NULL)) AS name
     FROM locations t0
  LEFT JOIN locations t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
  LEFT JOIN locations t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
  LEFT JOIN locations t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
  WHERE 
      (t0.feature_class IN ('P', 'A') OR (t0.feature_class = 'L' AND t0.feature_code = 'RGN' ) )
      AND t0.name like 'Cornwall%' 
  GROUP BY CONCAT_WS(', ', t0.name,
                     IF(t3.name != t0.name, t3.name, NULL),
                     IF(t2.name != t0.name, t2.name, NULL),
                     IF(t1.name != t0.name, t1.name, NULL))
  ORDER BY t0.name 

它返回错误的记录:

id      | name
---------------------------
2652355 | Cornwall, England
mysql sql group-by distinct mysql-5.7
3个回答
1
投票

我认为条件聚合应该可以解决问题。您可以通过name过滤记录,然后在聚合函数中应用逻辑。如果feature_code = 'RGN'存在记录,那么你想选择它,否则你会选择匹配记录中的最小id

SELECT IFNULL(MAX(CASE WHEN feature_code = 'RGN' THEN id END), MIN(id)) id_found
FROM mytable
WHERE name = @name;

Demo on DB Fiddle在搜索'Cornwall'时:

| id_found |
| -------- |
| 11609029 |

注意:如果你想要整个匹配记录,一个解决方案是简单地用原始表格JOIN上面的结果集:

SELECT t.*
FROM mytable t
INNER JOIN (
    SELECT IFNULL(MAX(CASE WHEN feature_code = 'RGN' THEN id END), MIN(id)) id_found
    FROM mytable
    WHERE name = @name
) x ON x.id_found = t.id;

Demo

| id       | name     | admin1 | admin2 | admin3 | feature_class | feature_code |
| -------- | -------- | ------ | ------ | ------ | ------------- | ------------ |
| 11609029 | Cornwall | ENG    |        |        | L             | RGN          |

0
投票

在MySQL中,您可以使用相关子查询:

select l.*
from locations l
where l.id = (select l2.id
              from locations l2
              where l2.name = l.name
              order by (feature_code = 'RGN') desc,  -- put regions first
                       id asc
             );

在MySQL 8+中,您还可以使用row_number()

select l.*
from (select l.*,
             row_number() over (partition by name 
                                order by (feature_code = 'RGN') desc, id
                               ) as seqnum
      from locations l
     ) l
where seqnum = 1;

0
投票

一种方法可以存在并且联合所有

select t1.* from location t1
where exists ( select 1 from location t2 where t2.name=t1.name and t2.feature_code='RGN'
             )
 and t1.feature_code='RGN'
union all

select t1.* from location t1
where not exists ( select 1 from location t2 where t2.name=t1.name and 
                t2.feature_code='RGN'
                  )
  and t1.id=(select min(id) from location t2 where t2.name=t1.name)
© www.soinside.com 2019 - 2024. All rights reserved.