MySQL - Conditional MIN MAX to return distinct record

∥☆過路亽.° 提交于 2019-12-14 03:05:57

问题


I have a database dump from the geonames website for Great Britain. It consists of approx 60000 records. example data is as follows:

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

The first record with feature code ADM2 means it is administrative level 2 The secord record with feature code RGN means it is a region.

I want to search for records by place names to build an autocomplete feature. Where records have the same name and if one of those records is a region i.e. has feature_code RGN then I want to return only that record otherwise I want to return the record which matches that name that has the lowest id.

I have tried the following but it doesn't work:

   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 

It returns the incorrect record:

id      | name
---------------------------
2652355 | Cornwall, England

回答1:


I think that conditional aggregation should do the trick. You can filter records by name, then apply the logic within aggregate functions. If a record exists with feature_code = 'RGN' then you want to select it, else you would pick the minimum id in matching record.

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

Demo on DB Fiddle when searching for 'Cornwall':

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

NB : if you want the whole matching record, one solution is to simply JOIN the above resultset with the original table:

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          |



回答2:


In MySQL, you can use a correlated subquery:

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
             );

In MySQL 8+, you can also use 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;



回答3:


one approach could be exists and union all

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)


来源:https://stackoverflow.com/questions/54960930/mysql-conditional-min-max-to-return-distinct-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!