

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 Amazon Keyspaces 的查询中将 `IN` 运算符与 `SELECT` 语句配合使用
<a name="in.select"></a>

**SELECT IN**

您可以使用 `SELECT` 语句查询表中的数据，这将读取表中一行或多行的一列或多列，并返回包含与请求匹配的行的结果集。`SELECT` 语句包含一个 `select_clause`，该子句用于确定读取并在结果集中返回哪些列。该子句可包含在返回数据之前对数据进行转换的指令。可选的 `WHERE` 子句指定必须查询哪些行，该子句由作为主键一部分的列上的关系组成。Amazon Keyspaces 支持在 `IN` 子句中使用 `WHERE` 关键字。本节通过示例来说明 Amazon Keyspaces 如何处理包含 `IN` 关键字的 `SELECT` 语句。

以下示例展示了 Amazon Keyspaces 如何将包含 `IN` 关键字的 `SELECT` 语句分解为*子查询*。在此示例中，我们使用名为 `my_keyspace.customers` 的表。此表包含一个主键列 `department_id`、两个聚类列 `sales_region_id` 和 `sales_representative_id`，以及在 `customer_name` 列中包含客户姓名的一个列。

```
SELECT * FROM my_keyspace.customers;

         department_id | sales_region_id | sales_representative_id | customer_name
        ---------------+-----------------+-------------------------+--------------
          0            |        0        |            0            |    a
          0            |        0        |            1            |    b
          0            |        1        |            0            |    c
          0            |        1        |            1            |    d
          1            |        0        |            0            |    e
          1            |        0        |            1            |    f
          1            |        1        |            0            |    g
          1            |        1        |            1            |    h
```

使用此表，您可以通过在 `WHERE` 子句中使用 `IN` 关键字，运行以下 `SELECT` 语句来查找您感兴趣的部门和销售区域中的客户。下面是一个示例语句。

```
SELECT * FROM my_keyspace.customers WHERE department_id IN (0, 1) AND sales_region_id IN (0, 1);
```

Amazon Keyspaces 将此语句分为四个子查询，如以下输出所示。

```
SELECT * FROM my_keyspace.customers WHERE department_id = 0 AND sales_region_id = 0;

 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  0            |        0        |           0             |    a
  0            |        0        |           1             |    b

SELECT * FROM my_keyspace.customers WHERE department_id = 0 AND sales_region_id = 1;

 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  0            |        1        |          0              |    c
  0            |        1        |          1              |    d

SELECT * FROM my_keyspace.customers WHERE department_id = 1 AND sales_region_id = 0;

 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  1            |        0        |          0              |    e
  1            |        0        |          1              |    f

SELECT * FROM my_keyspace.customers WHERE department_id = 1 AND sales_region_id = 1;

 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  1            |        1        |           0             |    g
  1            |        1        |           1             |    h
```

使用 `IN` 关键字时，在以下任一情况下，Amazon Keyspaces 都会自动对结果进行分页：
+ 每处理 10 个子查询之后。
+ 处理 1MB 的逻辑 IO 之后。
+ 如果您配置了 `PAGE SIZE`，则 Amazon Keyspaces 会在根据设置的 `PAGE SIZE` 读取要处理的查询数量后进行分页。
+ 如果您使用 `LIMIT` 关键字来减少返回的行数，则 Amazon Keyspaces 会在根据设置的 `LIMIT` 读取要处理的查询数量后进行分页。

 下表通过一个示例来具体说明。

有关分页的更多信息，请参阅[对 Amazon Keyspaces 中的结果进行分页](paginating-results.md)。

```
SELECT * FROM my_keyspace.customers;

         department_id | sales_region_id | sales_representative_id | customer_name
        ---------------+-----------------+-------------------------+--------------
          2            |        0        |          0              |    g
          2            |        1        |          1              |    h
          2            |        2        |          2              |    i
          0            |        0        |          0              |    a
          0            |        1        |          1              |    b
          0            |        2        |          2              |    c
          1            |        0        |          0              |    d
          1            |        1        |          1              |    e
          1            |        2        |          2              |    f
          3            |        0        |          0              |    j
          3            |        1        |          1              |    k
          3            |        2        |          2              |    l
```

您可以对此表运行以下语句来查看分页的运作方式。

```
SELECT * FROM my_keyspace.customers WHERE department_id IN (0, 1, 2, 3) AND sales_region_id IN (0, 1, 2) AND sales_representative_id IN (0, 1);
```

Amazon Keyspaces 将此语句处理为 24 个子查询，因为此查询中包含的所有 `IN` 词语的笛卡尔乘积的基数为 24。

```
 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  0            |        0        |          0              |    a
  0            |        1        |          1              |    b
  1            |        0        |          0              |    d
  1            |        1        |          1              |    e

---MORE---
 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  2            |        0        |          0              |    g
  2            |        1        |          1              |    h
  3            |        0        |          0              |    j

---MORE---
 department_id | sales_region_id | sales_representative_id | customer_name
---------------+-----------------+-------------------------+--------------
  3            |        1        |          1              |    k
```

以下示例展示了如何在包含 `IN` 关键字的 `SELECT` 语句中使用 `ORDER BY` 子句。

```
SELECT * FROM my_keyspace.customers WHERE department_id IN (3, 2, 1) ORDER BY sales_region_id DESC;
        
         department_id | sales_region_id | sales_representative_id | customer_name
        ---------------+-----------------+-------------------------+--------------
          3            |        2        |          2              |    l
          3            |        1        |          1              |    k
          3            |        0        |          0              |    j
          2            |        2        |          2              |    i
          2            |        1        |          1              |    h
          2            |        0        |          0              |    g
          1            |        2        |          2              |    f
          1            |        1        |          1              |    e
          1            |        0        |          0              |    d
```

子查询按照查询中显示分区键和聚类键列的顺序进行处理。在下面的示例中，首先处理分区键值为“2”的子查询，然后处理分区键值为“3”和“1”的子查询。给定子查询的结果根据查询的排序子句（如有）或表创建期间定义的表的聚类顺序进行排序。

```
SELECT * FROM my_keyspace.customers WHERE department_id IN (2, 3, 1) ORDER BY sales_region_id DESC;

         department_id | sales_region_id | sales_representative_id | customer_name
        ---------------+-----------------+-------------------------+--------------
          2            |        2        |          2              |    i
          2            |        1        |          1              |    h
          2            |        0        |          0              |    g
          3            |        2        |          2              |    l
          3            |        1        |          1              |    k
          3            |        0        |          0              |    j
          1            |        2        |          2              |    f
          1            |        1        |          1              |    e
          1            |        0        |          0              |    d
```