3

数字の文字列を含むフィールドがあります

 "2002 2005 2001 2006 2008 2344"

スペースが分割であると想定することをお勧めします。それ以外の場合は、それぞれの長さが 4 文字であると想定します。

部分文字列を選択できます:

SELECT substr(years,1,4)  FROM TABLE 

しかし、それぞれを確認する方法がわかりません。0 に最も近い数値を含む行を見つけようとしています。

ORDER BYそして私はできLIMIT 1ます。

4

2 に答える 2

2

これは醜く見えますが、1 つの SQLlite ステートメントで必要なものをすべて選択します。また、日付が1900..2100などの現在の間隔にあると仮定すると、最適化を行うことができます。この場合、最初の 2 つの選択 B、C (B: 1 ユニオン 2) (C: 9 ユニオン 0 ユニオン 1) をカットできます。

 select years2,years from 
    (
    select i1||i2||i3||i4  as years2 from (select '0' as i1 
                   union all 
                   select '1' as i1
                   union all 
                   select '2' as i1
                   union all 
                   select '3' as i1
                   union all 
                   select '4' as i1 
                   union all 
                   select '5' as i1 
                   union all 
                   select '6' as i1
                   union all 
                   select '7' as i1
                   union all 
                   select '8' as i1
                   union all 
                   select '9' as i1) B, 
    (select '0' as i2 
                   union all 
                   select '1' as i2
                   union all 
                   select '2' as i2
                   union all 
                   select '3' as i2
                   union all 
                   select '4' as i2 
                   union all 
                   select '5' as i2 
                   union all 
                   select '6' as i2
                   union all 
                   select '7' as i2
                   union all 
                   select '8' as i2
                   union all 
                   select '9' as i2) C,
    (select '0' as i3 
                   union all 
                   select '1' as i3
                   union all 
                   select '2' as i3
                   union all 
                   select '3' as i3
                   union all 
                   select '4' as i3 
                   union all 
                   select '5' as i3 
                   union all 
                   select '6' as i3
                   union all 
                   select '7' as i3
                   union all 
                   select '8' as i3
                   union all 
                   select '9' as i3) D,
                  (select '0' as i4 
                   union all 
                   select '1' as i4
                   union all 
                   select '2' as i4
                   union all 
                   select '3' as i4 
                   union all 
                   select '4' as i4 
                   union all 
                   select '5' as i4 
                   union all 
                   select '6' as i4
                   union all 
                   select '7' as i4
                   union all 
                   select '8' as i4
                   union all 
                   select '9' as i4) E
    ) YearsAll

   left join YearsTable on (YearsTable.years like '%'||YearsAll.years2||'%')

    where YearsTable.years is not null

    order by years2 limit 1
于 2012-08-01T08:27:19.007 に答える