admin管理员组文章数量:1401644
I want to select all rows that match at least one of given tags from this table:
If I want to search by tag1 and tag3 it should return 1st, 3rd and 4th row and when I want to search by tag2 and tag4 it should return all of them because they all have this tag and if I want to search by tag4 it should return just 2nd row.
I found how to select ones that have all the tags provided which is:
.select().contains("tags", ["tag1", "tag2"])
but the returned rows meet all two tags at once so it returns just 2nd and 3rd row and I want it to return 4th row as well.
I want to select all rows that match at least one of given tags from this table:
If I want to search by tag1 and tag3 it should return 1st, 3rd and 4th row and when I want to search by tag2 and tag4 it should return all of them because they all have this tag and if I want to search by tag4 it should return just 2nd row.
I found how to select ones that have all the tags provided which is:
.select().contains("tags", ["tag1", "tag2"])
but the returned rows meet all two tags at once so it returns just 2nd and 3rd row and I want it to return 4th row as well.
Share Improve this question edited Feb 16, 2023 at 21:25 pelak asked Feb 16, 2023 at 19:01 pelakpelak 681 silver badge7 bronze badges 2- Array.some() is one such function which return true if there is at least one element matching. Check this documentation – shanmukha vangaru Commented Feb 16, 2023 at 19:12
- I would rather know how to do it straight from supabase but I think it will do just fine if I make the logic behind it good enough – pelak Commented Feb 16, 2023 at 19:19
2 Answers
Reset to default 6I think you're looking for the overlap
filter
https://supabase./docs/reference/javascript/overlaps
const { data, error } = await supabase
.from('your_table')
.select()
.overlaps('tags', ['tag1', 'tag2'])
If you want to match any value of that array, then you need to encapsulate it within the OR filter:
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
.from('test_tags')
.select()
.or('tags.cs.{tag1}','tags.cs.{tag2}');
console.log(ret);
You can also expand and use two or more OR
logical operators:
const { data: ret, error } = await supabase
.from('test_tags')
.select()
.or('tags.cs.{tag1},or(tags.cs.{tag4},tags.cs.{tag3})');
console.log(JSON.stringify(ret));
本文标签: javascriptHow do I select rows that match any value inside an array in supabaseStack Overflow
版权声明:本文标题:javascript - How do I select rows that match any value inside an array in supabase? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744308367a2599909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论