Orientdb 简明教程
OrientDB - Display Records
类似于 RDBMS,OrientDB 支持不同的 SQL 查询类型以从数据库中检索记录。在检索记录时,我们有不同的查询变体或选项以及 select 语句。
Similar to RDBMS, OrientDB supports different types of SQL queries to retrieve the records from the database. While retrieving the records we have different variations or options of queries along with the select statement.
以下语句是 SELECT 命令的基本语法。
The following statement is the basic syntax of the SELECT command.
SELECT [ <Projections> ] [ FROM <Target> [ LET <Assignment>* ] ]
[ WHERE <Condition>* ]
[ GROUP BY <Field>* ]
[ ORDER BY <Fields>* [ ASC|DESC ] * ]
[ UNWIND <Field>* ]
[ SKIP <SkipRecords> ]
[ LIMIT <MaxRecords> ]
[ FETCHPLAN <FetchPlan> ]
[ TIMEOUT <Timeout> [ <STRATEGY> ] ]
[ LOCK default|record ]
[ PARALLEL ]
[ NOCACHE ]
以下是上文中选项的详细信息。
Following are the details about the options in the above syntax.
<Projections> - 指示您希望从查询中提取作为结果记录集的数据。
<Projections> − Indicates the data you want to extract from the query as a result records set.
FROM - 表示要查询的对象。这可以是类、集群、单个记录 ID、记录 ID 集合。您可以将所有这些对象指定为目标。
FROM − Indicates the object to query. This can be a class, cluster, single Record ID, set of Record IDs. You can specify all these objects as target.
WHERE - 指定用于过滤结果集的条件。
WHERE − Specifies the condition to filter the result-set.
LET - 表示用于投影、条件或子查询的上下文变量。
LET − Indicates the context variable which are used in projections, conditions or sub queries.
GROUP BY - 表示用于对记录进行分组的字段。
GROUP BY − Indicates the field to group the records.
ORDER BY - 表示用于按顺序排列记录的文件。
ORDER BY − Indicates the filed to arrange a record in order.
UNWIND - 指定用于解开记录集合的字段。
UNWIND − Designates the field on which to unwind the collection of records.
SKIP - 定义从结果集开始要跳过的记录数。
SKIP − Defines the number of records you want to skip from the start of the result-set.
LIMIT - 表示结果集中记录的最大数量。
LIMIT − Indicates the maximum number of records in the result-set.
FETCHPLAN - 指定定义如何获取结果的策略。
FETCHPLAN − Specifies the strategy defining how you want to fetch results.
TIMEOUT - 定义查询的最大时间(以毫秒为单位)。
TIMEOUT − Defines the maximum time in milliseconds for the query.
LOCK - 定义锁定策略。DEFAULT 和 RECORD 是可用的锁定策略。
LOCK − Defines the locking strategy. DEFAULT and RECORD are the available lock strategies.
PARALLEL - 执行针对“x”个并发线程的查询。
PARALLEL − Executes the query against ‘x’ concurrent threads.
NOCACHE − 定义是否要使用缓存。
NOCACHE − Defines whether you want to use cache or not.
Example
让我们考虑一下上一章中创建的以下 Customer 表。
Let’s consider the following Customer table created in the previous chapter.
Sr.No. |
Name |
Age |
1 |
Satish |
25 |
2 |
Krishna |
26 |
3 |
Kiran |
29 |
4 |
Javeed |
21 |
5 |
Raja |
29 |
尝试不同的 select 查询以从 Customer 表中检索数据记录。
Try different select queries to retrieve the data records from the Customer table.
Method 1 − 您可以使用以下查询从 Customer 表中选择所有记录。
Method 1 − You can use the following query to select all records from the Customer table.
orientdb {db = demo}> SELECT FROM Customer
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+-----+--------+----+-------+----
# |@RID |@CLASS |id |name |age
----+-----+--------+----+-------+----
0 |#11:0|Customer|1 |satish |25
1 |#11:1|Customer|2 |krishna|26
2 |#11:2|Customer|3 |kiran |29
3 |#11:3|Customer|4 |javeed |21
4 |#11:4|Customer|5 |raja |29
----+-----+--------+----+-------+----
Method 2 − 选择所有名称以字母“ k ”开头的记录。
Method 2 − Select all records whose name starts with the letter 'k'.
orientdb {db = demo}> SELECT FROM Customer WHERE name LIKE 'k%'
或者您可以对上述示例使用以下查询。
OR you can use the following query for the above example.
orientdb {db = demo}> SELECT FROM Customer WHERE name.left(1) = 'k'
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+-----+--------+----+-------+----
# |@RID |@CLASS |id |name |age
----+-----+--------+----+-------+----
0 |#11:1|Customer|2 |krishna|26
1 |#11:2|Customer|3 |kiran |29
----+-----+--------+----+-------+----
Method 3 − 从 Customer 表中选择 id、name 记录,这些记录中的名称为大写字母。
Method 3 − Select id, name records from the Customer table with names in uppercase letters.
orientdb {db = demo}> SELECT id, name.toUpperCase() FROM Customer
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+--------+----+-------
# |@CLASS |id |name
----+--------+----+-------
0 |null |1 |SATISH
1 |null |2 |KRISHNA
2 |null |3 |KIRAN
3 |null |4 |JAVEED
4 |null |5 |RAJA
----+--------+----+-------
Method 4 − 从 Customer 表中选择所有记录,其中年龄在 25 至 29 岁之间。
Method 4 − Select all records from the Customer table where age is in the range of 25 to 29.
orientdb {db = demo}> SELECT FROM Customer WHERE age in [25,29]
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+-----+--------+----+-------+----
# |@RID |@CLASS |id |name |age
----+-----+--------+----+-------+----
0 |#11:0|Customer|1 |satish |25
1 |#11:2|Customer|3 |kiran |29
2 |#11:4|Customer|5 |raja |29
----+-----+--------+----+-------+----
Method 5 − 从 Customer 表中选择所有记录,其中任何字段都包含单词“sh”。
Method 5 − Select all records from the Customer table where any field contains the word ‘sh’.
orientdb {db = demo}> SELECT FROM Customer WHERE ANY() LIKE '%sh%'
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+-----+--------+----+-------+----
# |@RID |@CLASS |id |name |age
----+-----+--------+----+-------+----
0 |#11:0|Customer|1 |satish |25
1 |#11:1|Customer|2 |krishna|26
----+-----+--------+----+-------+----
Method 6 − 从 Customer 表中选择按年龄降序排列的所有记录。
Method 6 − Select all records from the Customer table, ordered by age in descending order.
orientdb {db = demo}> SELECT FROM Customer ORDER BY age DESC
如果成功执行了以上查询,您会获得以下输出。
If the above query is executed successfully, you will get the following output.
----+-----+--------+----+-------+----
# |@RID |@CLASS |id |name |age
----+-----+--------+----+-------+----
0 |#11:2|Customer|3 |kiran |29
1 |#11:4|Customer|5 |raja |29
2 |#11:1|Customer|2 |krishna|26
3 |#11:0|Customer|1 |satish |25
4 |#11:3|Customer|4 |javeed |21
----+-----+--------+----+-------+----