> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> ClickHouse IPv4 数据类型文档

# IPv4

<div id="ipv4">
  ## IPv4
</div>

IPv4 地址。以 4 字节的 UInt32 存储。

<div id="basic-usage">
  ### 基本用法
</div>

```sql theme={null}
CREATE TABLE hits (url String, from IPv4) ENGINE = MergeTree() ORDER BY url;

DESCRIBE TABLE hits;
```

```text theme={null}
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┐
│ url  │ String │              │                    │         │                  │
│ from │ IPv4   │              │                    │         │                  │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┘
```

或者，也可以使用 IPv4 域类型作为键：

```sql theme={null}
CREATE TABLE hits (url String, from IPv4) ENGINE = MergeTree() ORDER BY from;
```

`IPv4` domain 支持将 IPv4 字符串用作自定义输入格式：

```sql theme={null}
INSERT INTO hits (url, from) VALUES ('https://wikipedia.org', '116.253.40.133')('https://clickhouse.com', '183.247.232.58')('https://clickhouse.com/docs/en/', '116.106.34.242');

SELECT * FROM hits;
```

```text theme={null}
┌─url────────────────────────────────┬───────────from─┐
│ https://clickhouse.com/docs/en/ │ 116.106.34.242 │
│ https://wikipedia.org              │ 116.253.40.133 │
│ https://clickhouse.com          │ 183.247.232.58 │
└────────────────────────────────────┴────────────────┘
```

值以紧凑的二进制格式存储：

```sql theme={null}
SELECT toTypeName(from), hex(from) FROM hits LIMIT 1;
```

```text theme={null}
┌─toTypeName(from)─┬─hex(from)─┐
│ IPv4             │ B7F7E83A  │
└──────────────────┴───────────┘
```

IPv4 地址可以直接与 IPv6 地址进行比较：

```sql theme={null}
SELECT toIPv4('127.0.0.1') = toIPv6('::ffff:127.0.0.1');
```

```text theme={null}
┌─equals(toIPv4('127.0.0.1'), toIPv6('::ffff:127.0.0.1'))─┐
│                                                       1 │
└─────────────────────────────────────────────────────────┘
```

**另请参阅**

* [处理 IPv4 和 IPv6 地址的函数](/zh/reference/functions/regular-functions/ip-address-functions)
