Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/scripts/sql/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can use SQL to query data in SeaTable. If some tables in a base are archived

Currently only `select`, `insert`, `update`, and `delete` statements are supported. (`insert`, `update`, and `delete` statements require version 2.7 or later)

The synatx of `select` statement is:
The syntax of `select` statement is:

```
SELECT [DISTINCT] fields FROM table_name [WhereClause] [GroupByClause] [HavingClause] [OrderByClause] [Limit Option]
Expand Down Expand Up @@ -37,14 +37,14 @@ Notes:
- `GROUP BY` uses strict syntax. The selected fields must appear in group by list, except for aggregation functions (`COUNT`, `SUM`, `MAX`, `MIN`, `AVG`) and formulas (see extended syntax section below).
- `HAVING` filters rows resulting from the group by clause. Only fields referred in the "GROUP BY" clause or aggregation functions (such as "SUM") can be used in having clause. Other syntax is the same as specified for the where clause.
- Fields in "order by" list must be a column or an expression in the selected fields. For example, `select a from table order by b` is invalid; while `select a from table order by b` and `select abs(a), b from table order by abs(a)` are valid.
- Limit options are in MySQL format. The general syntax is `LIMIT ... OFFSET ...`. You may obmit `LIMIT` or `OFFSET`.
- Limit options are in MySQL format. The general syntax is `LIMIT ... OFFSET ...`. You may omit `LIMIT` or `OFFSET`.
- Field alias with `AS` syntax is supported. For example, `select table.a as a from table` returns rows whose first column is keyed by "a". There are two notes:
- Field alias can be referred in `group by`, `having` and `order by` clauses. E.g., `select t.registration as r, count(*) as c from t group by r having c > 100` is valid.
- Field alias cannot be referred in `where` clause. E.g., `select t.registration as r, count(*) from t group by r where r > "2020-01-01"` will report syntax error.

Each returned row is a JSON map. The keys of the maps are the column keys, NOT column names. To use column names as keys, the `convert_keys` parameter (available since version 2.4) in query request should be TRUE. For JOIN query, the keys of row maps match the "id" fields (not the "key" or the "name"). Those column fields (e.g. id, key, name) are returned under `metadata` array in query response.

The synatx of `insert`, `update`, and `delete` statements are:
The syntax of `insert`, `update`, and `delete` statements are:

```
INSERT INTO table_name [column_list] VALUES value_list [, ...]
Expand Down Expand Up @@ -75,20 +75,20 @@ Below is mapping from SeaTable column types to SQL column types.
| SeaTable Column Type | SQL Column Type | Query result format | Use in WHERE clause | Use in GROUP BY / ORDER BY clause |
| :-------------------- | :--------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------- |
| text | String | | Supported | Supported. |
| long-text | String | Raw text in Markdown formst | Supported | Supported |
| long-text | String | Raw text in Markdown forms | Supported | Supported |
| number | Float | | Supported | Supported |
| single-select | String | Returned rows contain the option key by default. To return the option name, the `convert_keys` parameter (available since version 2.4) in query request should be TRUE. | Refer an option by its name. E.g. `where single_select = "New York"`. | Order by the definition order of the options |
| multiple-select | List of strings | Returned rows contain the option key by default. To return the option name, the `convert_keys` parameter (available since version 2.4) in query request should be TRUE. | Refer an option by its name. E.g. `where multi_select = "New York"`. More details in "List Types" section below. | More details in "List Types" section below. |
| checkbox | Bool | | Supported | Supported |
| date | Datetime | Time strings in RFC 3339 format | Constants are expressed in strings in ISO format. e.g. "2006-1-2" or "2006-1-2 15:04:05". Since 2.8 version, strings in RFC 3339 format are supported (such as "2020-12-31T23:59:60Z"). | Supported |
| image | List of URL for images | A JSON array with image URLs as elements | Supported. More details in "List Types" section below. | Supported. More details in "List Types" section below. |
| file | Will be returned as JSON format string when queried. | Not supported | Not Supported | Not Supported |
| collaborator | List of user IDs | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with seatable APIs. | Supported. More details in "List Types" section below. | Supported. More details in "List Types" section below. |
| collaborator | List of user IDs | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with Seatable APIs. | Supported. More details in "List Types" section below. | Supported. More details in "List Types" section below. |
| link to other records | List of linked rows | Supported. More details in "List Types" section below. | Supported. More details in "List Types" section below. | Supported. More details in "List Types" section below. |
| formula | The type depends on the return value of the formula. | Depends on the type of the return value | Depends on the type of the return value | Depends on the type of the return value |
| \_creator | User ID as stirng | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with seatable APIs. | Supported | Supported |
| \_creator | User ID as string | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with Seatable APIs. | Supported | Supported |
| \_ctime | Datetime | Time strings in RFC 3339 format | Constants are expressed in strings in ISO format. e.g. "2006-1-2" or "2006-1-2 15:04:05". Since 2.8 version, strings in RFC 3339 format are supported (such as "2020-12-31T23:59:60Z"). | Supported |
| \_last_modifier | User ID as string | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with seatable APIs. | Supported | Supported |
| \_last_modifier | User ID as string | Format is like 5758ecdce3e741ad81293a304b6d3388@auth.local. If you need user names, you have to convert with Seatable APIs. | Supported | Supported |
| \_mtime | Datetime | Time strings in RFC 3339 format | Constants are expressed in strings in ISO format. e.g. "2006-1-2" or "2006-1-2 15:04:05". Since 2.8 version, strings in RFC 3339 format are supported (such as "2020-12-31T23:59:60Z"). | Supported |
| auto number | String | | Supported | Supported. |
| url | String | | Supported | Supported. |
Expand Down Expand Up @@ -160,19 +160,19 @@ In formulas, NULL values will be converted to 0 or an empty strings.
You may use a formula syntax that's almost the same as SeaTable's formulas in SQL queries. There are a few special notes:

- Link formulas are not supported. e.g. {link.age} is invalid.
- Reference to columns should not be enclosed by curly brackets ("{}"). Don't write `select abs({column}) from talbe;`. Wirte `select abs(column) from table;`. This is consistent with standard SQL syntax.
- Reference to columns should not be enclosed by curly brackets ("{}"). Don't write `select abs({column}) from table;`. Write `select abs(column) from table;`. This is consistent with standard SQL syntax.
- You can use back quote ("\`\`") to enclose column references, when column name contains space or "-". E.g. select abs(`column-a`) from table;
- You may not use column alias in formulas. E.g. `select abs(t.column) from table as t;` is invalid.
- formulas can be use in group by and order by clauses.

A few extended formulas are supported:

- `STARTOFWEEK(date, weekStart)`: returns the first day of the week where "date" is in. "weekstart" can be used to choose "sunday" or "monday" as the first day of a week.
- `Quarter(date)`: Returns the quater of the date. Return value is 1, 2, 3 or 4.
- `Quarter(date)`: Returns the quarter of the date. Return value is 1, 2, 3 or 4.
- `ISODate(date)`: Returns ISO format string for the date. E.g. "2020-09-08".
- `ISOMonth(date)`: Returns ISO format string for the month where "date" is in. E.g. "07".

The above formulas can be used for group by week, quater, date and month. E.g. `select sum(sale) from SalesRecord group by ISODate(SalesTime);` will return the total sales amount for each day.
The above formulas can be used for group by week, quarter, date and month. E.g. `select sum(sale) from SalesRecord group by ISODate(SalesTime);` will return the total sales amount for each day.

For more details, please refer to \[./function.md].

Expand All @@ -188,5 +188,5 @@ To improve query performance, SeaTable will automatically create indexes for the

When you add or delete a column in a table, the index for this column is not added/removed immediately. Indexes creation and deletion are triggered in two cases:

1. When you archive the table for the next time, indexes are created for new columns and indexes for remvoed columns are removed.
1. When you archive the table for the next time, indexes are created for new columns and indexes for removed columns are removed.
2. Users may manage indexes from "index management" UI. You can open it from the "Big data management" menu in the base.