

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 Amazon Keyspaces 的查詢中使用 `IN`運算子搭配 `SELECT`陳述式
<a name="in.select"></a>

**在 中選取**

您可以使用 `SELECT`陳述式從資料表查詢資料，該陳述式會讀取資料表中一或多個資料列的一或多個資料欄，並傳回結果集，其中包含符合請求的資料列。`SELECT` 陳述式包含 `select_clause`，決定要在結果集中讀取和傳回哪些資料欄。子句可以包含在傳回資料之前轉換資料的指示。選用的 `WHERE` 子句會指定哪些資料列必須查詢，並由屬於主索引鍵之資料欄上的關係組成。Amazon Keyspaces 支援 `WHERE`子句中的 `IN` 關鍵字。本節使用範例來示範 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
```

使用此資料表，您可以執行下列`SELECT`陳述式，使用 `WHERE`子句中的`IN`關鍵字來尋找您感興趣的部門和銷售區域中的客戶。下列陳述式為範例。

```
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
```