把 SQL 转成 JavaScript API
许多常见的 SQL 查询可以使用 SDK 提供的 JavaScript API 来编写,该 API 用于封装 Data API 调用。下面是一些 SQL 与 JavaScript 模式转换的例子。
🌐 Many common SQL queries can be written using the JavaScript API, provided by the SDK to wrap Data API calls. Below are a few examples of conversions between SQL and JavaScript patterns.
带基本子句的选择语句 #
🌐 Select statement with basic clauses
从单个表中选择一组列,并使用 where、order by 和 limit 子句。
🌐 Select a set of columns from a single table with where, order by, and limit clauses.
select 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;const { 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 }) // or just .order('last_name') .order('first_name', { ascending: false }) .limit(20)带有复杂布尔逻辑子句的选择语句 #
🌐 Select statement with complex Boolean logic clause
从单个表中选择所有列,并使用复杂的 WHERE 子句:OR AND OR
🌐 Select all columns from a single table with a complex where clause: OR AND OR
select *from playerswhere ((team_id = 'CHN' or team_id is null) and (age > 35 or age is null));const { data, error } = await supabase .from('players') .select() // or .select('*') .or('team_id.eq.CHN,team_id.is.null') .or('age.gt.35,age.is.null') // additional filters imply "AND"从单个表中选择所有列,并使用复杂的 WHERE 子句:AND OR AND
🌐 Select all columns from a single table with a complex where clause: AND OR AND
select *from playerswhere ((team_id = 'CHN' and age > 35) or (team_id != 'CHN' and age is not null));const { data, error } = await supabase .from('players') .select() // or .select('*') .or('and(team_id.eq.CHN,age.gt.35),and(team_id.neq.CHN,.not.age.is.null)')资源 #
🌐 Resources