SUPABASE 中带参数的复杂查询

Jav*_*nas 3 supabase

我想except在 supabase 中创建此查询,但在day列中使用参数。例如:

CREATE VIEW employees_without_registering AS 
  SELECT employees.id
  FROM employees
  EXCEPT
  SELECT "idEmployee" FROM registers WHERE "day"='2021-07-25'
Run Code Online (Sandbox Code Playgroud)

我尝试的是在查询编辑器中创建一个视图,然后尝试使用如下内容访问结果:

await supabase.from('employees_without_registering')
  .select('*');
Run Code Online (Sandbox Code Playgroud)

但结果仅限于该查询。我想根据我的选择改变这一天。

也许我的方法不好,可以通过使用supabase api的另一种方法来解决这个问题,但我想听听有关如何解决这个问题的建议。

ize*_*ze8 7

您需要为此编写一个存储函数并使用 supabase.rpc("get_my_complex_query",{parameter:1})

转到仪表板上的 SQL 查询编辑器,然后:

drop function if exists get_my_complex_query;
create function get_my_complex_query (parameter int)
RETURNS TABLE(column1 integer, column2 varchar) AS
$$
BEGIN
  return query SELECT column1, column2 FROM "TableName1"
    INNER JOIN "TableName2" ON "TableName1".column = "TableName2".column 
    WHERE "Table2".column = parameter;
END;
$$
language plpgsql volatile;
Run Code Online (Sandbox Code Playgroud)

以下是一些有用的链接,可了解更多详细信息:

https://www.postgresqltutorial.com/introduction-to-postgresql-stored-procedures/

https://www.postgresql.org/docs/9.1/plpgsql-declarations.html

https://www.postgresqltutorial.com/plpgsql-function-returns-a-table/

您的另一个选择是在客户端过滤您的视图:

await supabase.from('employees')
  .select('id')
  .neq("day","2021-07-25");
Run Code Online (Sandbox Code Playgroud)