2017-12-23 3 views
1

私は問題を抱えています。私はmysqlの初心者で、URLアドレスから値を抽出するクエリを作成するように割り当てられました。だから、基本的に私は最後の '/'から '最初の'? '(この場合は' search ')までのすべてを抽出する必要がある、例えばhttps://www.google.com/search?source=のような数千のURLアドレスを持っています。それは簡単ではなく、時にはhttpであることもありますが、時には '/'文字の数が同じではなく、時には間違ったアドレスです(私はそれらのケースを無視する必要があります)、時には普通の場合https://google.com/search 「?」(これらの例は、同様に無視する必要があります。私はここまでですが、私は、任意の提案を無力感を覚える?疑問符ため条件が満たされている場合のみ、特定の文字から特定の文字までを選択しますか?

select distinct SUBSTRING_INDEX(col,'/',0) col from table where length(col) - length(replace(col, '/', '')) >= 1 
     and length(col) - length(replace(col, '?', '')) >= 1 
       and col = 'value' 
        and col <> '' 
         and col is not null 
         order by date 
         limit 600; 
+0

は、有効なデータと無効なデータのサンプルを提供し、手動で準備し、そのデータから必要な「期待した結果」を提供します。 –

答えて

0

テスト第一。その後、それはその位置に部分文字列を使用して存在している場合「?前にスラッシュ」、見つけるために、逆に、私は/がどこにあるか知っているとしたら、私は、URLのその部分を抽出することができます?

select 
    url 
, substr(url,1,qmarkpos-1) 
, case when qmarkpos > 0 then instr(reverse(substr(url,1,qmarkpos-1)),'/') end 
, substr(substr(url,1,qmarkpos-1),qmarkpos-instr(reverse(substr(url,1,qmarkpos-1)),'/'),qmarkpos) 
from (
     select url, instr(url,'?') qmarkpos 
     from (
      select 'https://www.google.com/search?source=' as url union all 
      select 'https://www.google.com/search' as url union all 
      select 'https://www.google.com/search?source=uqu iqu iugqidug iqugd' as url union all 
      select 'https://www.google.com/search?source/qh ohqod hi=' as url    
      ) d 
    ) d2 

+---+-------------------------------------------------------------+-------------------------------+--------------+---------+ 
| |        url        | substr(url,1,qmarkpos-1) | case ... end | result | 
+---+-------------------------------------------------------------+-------------------------------+--------------+---------+ 
| 1 | https://www.google.com/search?source=      | https://www.google.com/search | 7   | /search | 
| 2 | https://www.google.com/search        |        | NULL   |   | 
| 3 | https://www.google.com/search?source=uqu iqu iugqidug iqugd | https://www.google.com/search | 7   | /search | 
| 4 | https://www.google.com/search?source/qh ohqod hi=   | https://www.google.com/search | 7   | /search | 
+---+-------------------------------------------------------------+-------------------------------+--------------+---------+ 

Demo

+0

「https://www.google.com/search?source=」から申し訳ありませんが、www.google.comを抽出する必要があります。つまり、開始から終了までのすべてを削除する必要があります。// including://まで?または? www.google.comから250文字までのすべてが存在しません。 @Used_By_Already – Gudzo

+0

そのコメントが何であるか分かりません。 –

関連する問題