将SQL转换为JavaScript API
许多常见的SQL查询都可以使用JavaScript API编写,该API由SDK提供,用于封装Data API调用。以下是SQL与JavaScript模式转换的几个示例。
带基本子句的SELECT语句
使用where、order by和limit子句从单个表中选择一组列。
12345select first_name, last_name, team_id, agefrom playerswhere age between 20 and 24 and team_id != 'STL'order by last_name, first_name desclimit 20;
123456789const { data, error } = await supabase .from('players') .select('first_name,last_name,team_id,age') .gte('age', 20) .lte('age', 24) .not('team_id', 'eq', 'STL') .order('last_name', { ascending: true }) // 或简写为 .order('last_name') .order('first_name', { ascending: false }) .limit(20)
带复杂布尔逻辑子句的SELECT语句
从单个表中选择所有列,使用复杂的where子句:OR AND OR
123select *from playerswhere ((team_id = 'CHN' or team_id is null) and (age > 35 or age is null));
12345const { data, error } = await supabase .from('players') .select() // 或 .select('*') .or('team_id.eq.CHN,team_id.is.null') .or('age.gt.35,age.is.null') // 额外过滤器隐含"AND"关系
从单个表中选择所有列,使用复杂的where子句:AND OR AND
123select *from playerswhere ((team_id = 'CHN' and age > 35) or (team_id != 'CHN' and age is not null));
1234const { data, error } = await supabase .from('players') .select() // 或 .select('*') .or('and(team_id.eq.CHN,age.gt.35),and(team_id.neq.CHN,.not.age.is.null)')