如何从 supabase 表一次从多个表中提取数据?

问题描述 投票:0回答:1

我在 supabase 数据库上有一些表。
有一个 post 表,它包含 profiles 表中的 user_id。

user_id
与配置文件表的id相关的字段。
我正在使用
@supabase
节点模块从 supabase 中提取数据。
我可以通过单个查询拉取数据,但我不知道如何通过一个查询从多个表中拉取数据。
这是我尝试使用的代码。

await supabase
  .from('posts')
  .select(`user_id,
    profiles(
      first_name
    )
  `);

我该如何解决这个问题?

javascript supabase
1个回答
0
投票

要使用 Supabase 在单个查询中从多个表中获取数据,您可以在查询中使用 JOIN 子句。在您的情况下,您想使用

posts
字段将
profiles
表与
user_id
表连接起来。

await supabase
  .from('posts')
  .select('posts.title, posts.content, profiles.first_name')
  .innerJoin('profiles', 'posts.user_id', 'profiles.id');

您还可以使用其他类型的联接,例如

leftOuterJoin
rightOuterJoin
,具体取决于您的需要。此外,您可以使用
where
order
limit
等方法将各种过滤和排序选项应用于查询。

© www.soinside.com 2019 - 2024. All rights reserved.