5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FQLでlikeっぽく絞り込みを行う方法

5
Posted at

FQLでlikeを使ってみる

FQLにはlikeオペレータが無いのか、下記の様にクエリを投げるとErrorが返ってくる。
下記のクエリは、自分の友だちの中で「Sato」さんを取得したいときのサンプル。

like.sql
select uid, name from user where uid in (select uid2 from friend where uid1 = me()) and name like "%Sato%"
response.json
{
  "error": {
    "message": "(#601) Parser error: unexpected 'like' at position 93.", 
    "type": "OAuthException", 
    "code": 601
  }
}

Parser errorって返ってくるってことはやっぱりlike自体受け付けていないっぽい。
なんとかならないかと思って探してみたら、似たような機能を見つけた。

strposを使う

どうやらstrposという関数が使用できるらしい。
strposを使えば文字列の中で指定する文字列が該当した位置を教えてくれるため、下記の様に使用すれば文字列検索っぽいことが可能になる。

strpos.sql
select uid, name from user where uid in (select uid2 from friend where uid1 = me()) AND strpos(name,'Sato') >=0

これで、名前の中に「Sato」という文字列が存在している友達を取得できる。

注意点

あくまでも指定しているlocaleで検索をかけるらしい。
localeに「ja_JP」を指定して、日本語の名前を返すようにしていた時に、「Sato」で検索をかけても、日本語に対して検索するので、該当なしになってしまう。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?