# 条件查询

## 字段条件

* **布尔**:
  * \=, !=
* **数值**:
  * \=, !=, >, <, >=, <=,
  * IN, NOT IN
* **时间**:
  * \=, !=, >, <, >=, <=
  * IN, NOT IN
* **字符**:
  * \=, !=, >, <, >=, <=
  * IN, NOT IN
  * Contains, HasPrefix, HasSuffix
  * ContainsFold, EqualFold (**SQL** specific)
* **可选** 字段:
  * IsNil, NotNil

## 边条件

* **HasEdge**. 满足边的实体，例如：`Pet` 类型定义了 `owner` 边，要查询满足该边（有主人的宠物）的实体：

  ```go
   client.Pet.
        Query().
        Where(user.HasOwner()).
        All(ctx)
  ```
* **HasEdgeWith**. 满足边及其条件的实体列表，例如，在满足上一个例子的情况下，还要求主人的姓名为 `a8m`：

  ```go
   client.Pet.
        Query().
        Where(user.HasOwnerWith(user.Name("a8m"))).
        All(ctx)
  ```

## 非 (NOT)

```go
client.Pet.
    Query().
    Where(user.Not(user.NameHasPrefix("Ari"))).
    All(ctx)
```

## 或 (OR)

```go
client.Pet.
    Query().
    Where(
        user.Or(
            user.HasOwner(),
            user.Not(user.HasFriends()),
        )
    ).
    All(ctx)
```

## 与 (AND)

```go
client.Pet.
    Query().
    Where(
        user.And(
            user.HasOwner(),
            user.Not(user.HasFriends()),
        )
    ).
    All(ctx)
```

## 自定义条件

自定义条件，可以让你自行书写满足所使用方言的查询条件。

```go
pets := client.Pet.
    Query().
    Where(predicate.Pet(func(s *sql.Selector) {
        s.Where(sql.InInts(pet.OwnerColumn, 1, 2, 3))
    })).
    AllX(ctx)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://3ks.gitbook.io/ent-zh_cn/dai-ma-sheng-cheng/predicates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
