简体   繁体   English

MYSQL SUBSTRING_INDEX / CONVERT / IF子句

[英]MYSQL SUBSTRING_INDEX / CONVERT / IF clause

I have a table with something like this. 我有这样的桌子。

abc-cde-0001222
abc-cde-0001223
abc-ced-0001224
cde-efg-0003001
ced-efg-0003002
ced-efg-0003008

I wish to select abc-cde-0001222, abc-ced-0001224 我希望选择abc-cde-0001222, abc-ced-0001224

        also cde-efg-0003001 , cde-efg-0003002

             ced-efg-0003008  , ""

See, I need to obtain 1st and last values of a range. 看,我需要获取范围的第一个和最后一个值。 If the particular value is only one item (not in a range) I want it to select as the 1st value and a null value. 如果特定值只是一项(不在范围内),我希望它选择为第一值和空值。

So far I have only a Idea of using IF and increment operator (If this is possible). 到目前为止,我只有使用IFincrement operator的想法(如果可能的话)。 I can do the formatting and obtain the Int value using 我可以使用以下格式进行格式化并获取Int值

SELECT field, CONVERT(SUBSTRING_INDEX(field,'-',-1), UNSIGNED INTEGER) AS num
FROM table
ORDER BY num;

Please could you give me a hand... Lot appreciated 请你帮我一下...非常感谢

This is a pain in MySQL. 这是MySQL的痛苦。 But, it is possible. 但是,有可能。 The logic is to find where a sequence starts. 逻辑是查找序列从何处开始。 You can do this with a correlated subquery and some arithmetic. 您可以使用相关的子查询和一些算法来做到这一点。 Then you want a cumulative sum of the starts. 然后,您需要起点的累积总和。 This is harder, because it requires doing a correlated subquery on the correlated subquery. 这比较困难,因为它需要对相关子查询执行相关子查询。

First, to set the NullIfStart variable: 首先,设置NullIfStart变量:

select t.*,
       (select 0
        from t t2
        where left(t2.field, 8) = left(t.field, 8) and
              t2.field < t.field and
              right(t2.field, 7) + 0 = right(t.field, 7) + 0 - 1
       ) as NullIfStart
from t;

This gets the cumulative sum, which can be used as a grouping field: 这将获取累积总和,可以将其用作分组字段:

select t.*
       (select sum(NullIfStart is null)
        from (select t1.*,
                     (select 0
                      from t t2
                      where left(t2.field, 8) = left(t1.field, 8) and
                            t2.field < t1.field and
                            right(t2.field, 7) + 0 = right(t1.field, 7) + 0 - 1
                     ) as NullIfStart
               from t t1
              ) tnis
         where right(tnis.field, 7) = right(t.field, 7) and
               tnis.field <= t.field
        ) grp
from t;

The final solution is to do the aggregation: 最终的解决方案是进行聚合:

select min(field),
       (case when max(field <> min(field) then max(field) end)
from (select t.*
             (select sum(NullIfStart is null)
              from (select t1.*,
                           (select 0
                            from t t2
                            where left(t2.field, 8) = left(t1.field, 8) and
                                  t2.field < t1.field and
                                  right(t2.field, 7) + 0 = right(t1.field, 7) + 0 - 1
                           ) as NullIfStart
                     from t t1
                    ) tnis
               where left(tnis.field, 8) = left(t.field, 8) and
                     tnis.field <= t.field
              ) grp
      from t
     ) s
group by left(s.field, 8), grp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM