Ahlin Chan
01/26/2022, 2:43 PMsql
select id from servers where count(select * from server_bots where servers.id = server_bots.id) < 25;
Using @supabase/supabase-js
What i want to achieve is that i want to obtain an id for a server which has less than 25 bots registered in it. To do this with SQL i can use count(...)
which will count the entries that has the same server id.
I'm aware i can retrieve the entries in server_bots
that has the same server id then i count them using if statement in JavaScript; and i repeat till i get a server id that points to a server that has less than 25 bots, but this practice seems to be bad as it will be expensive when the number of servers increase.
i want to know what is better way to do this Using @supabase/supabase-js
, thank you!.silentworks
01/26/2022, 2:50 PMsilentworks
01/26/2022, 2:51 PMsilentworks
01/26/2022, 2:53 PMsql
CREATE VIEW public.servers_with_less_than_25_bots AS select id from servers where count(select * from server_bots where servers.id = server_bots.id) < 25;
Then you can call it with the js library
js
await supabase.from('servers_with_less_than_25_bots').select('id')