2022-08-26 19:07:59 +00:00
|
|
|
|
---
|
|
|
|
|
slug: /zh/sql-reference/table-functions/mysql
|
|
|
|
|
---
|
2020-04-03 13:23:32 +00:00
|
|
|
|
# mysql {#mysql}
|
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
允许对存储在远程MySQL服务器上的数据执行`SELECT`和`INSERT`查询。
|
|
|
|
|
|
|
|
|
|
**语法**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
mysql('host:port', 'database', 'table', 'user', 'password'[, replace_query, 'on_duplicate_clause']);
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**参数**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `host:port` — MySQL服务器地址.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `database` — 远程数据库名称.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `table` — 远程表名称.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `user` — MySQL用户.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `password` — 用户密码.
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `replace_query` — 将INSERT INTO` 查询转换为 `REPLACE INTO`的标志。如果 `replace_query=1`,查询被替换。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
- `on_duplicate_clause` — 添加 `ON DUPLICATE KEY on_duplicate_clause` 表达式到 `INSERT` 查询。明确规定只能使用 `replace_query = 0` ,如果你同时设置replace_query = 1`和`on_duplicate_clause`,ClickHouse将产生异常。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
示例:`INSERT INTO t (c1,c2) VALUES ('a', 2) ON DUPLICATE KEY UPDATE c2 = c2 + 1`
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
`on_duplicate_clause`这里是`UPDATE c2 = c2 + 1`。请查阅MySQL文档,来找到可以和`ON DUPLICATE KEY`一起使用的 `on_duplicate_clause`子句。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
简单的 `WHERE` 子句如 `=, !=, >, >=, <, <=` 将即时在MySQL服务器上执行。其余的条件和 `LIMIT` 只有在对MySQL的查询完成后,才会在ClickHouse中执行采样约束。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
支持使用`|`并列进行多副本查询,示例如下:
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT name FROM mysql(`mysql{1|2|3}:3306`, 'mysql_database', 'mysql_table', 'user', 'password');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
或
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT name FROM mysql(`mysql1:3306|mysql2:3306|mysql3:3306`, 'mysql_database', 'mysql_table', 'user', 'password');
|
|
|
|
|
```
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**返回值**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
与原始MySQL表具有相同列的表对象。
|
|
|
|
|
|
2023-03-03 10:35:07 +00:00
|
|
|
|
:::note
|
|
|
|
|
在`INSERT`查询中为了区分`mysql(...)`与带有列名列表的表名的表函数,你必须使用关键字`FUNCTION`或`TABLE FUNCTION`。查看如下示例。
|
|
|
|
|
:::
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## 用法示例 {#usage-example}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
MySQL中的表:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` text
|
|
|
|
|
mysql> CREATE TABLE `test`.`test` (
|
|
|
|
|
-> `int_id` INT NOT NULL AUTO_INCREMENT,
|
|
|
|
|
-> `int_nullable` INT NULL DEFAULT NULL,
|
|
|
|
|
-> `float` FLOAT NOT NULL,
|
|
|
|
|
-> `float_nullable` FLOAT NULL DEFAULT NULL,
|
|
|
|
|
-> PRIMARY KEY (`int_id`));
|
|
|
|
|
Query OK, 0 rows affected (0,09 sec)
|
|
|
|
|
|
|
|
|
|
mysql> insert into test (`int_id`, `float`) VALUES (1,2);
|
|
|
|
|
Query OK, 1 row affected (0,00 sec)
|
|
|
|
|
|
|
|
|
|
mysql> select * from test;
|
2020-04-08 14:22:25 +00:00
|
|
|
|
+------+----------+-----+----------+
|
2020-04-03 13:23:32 +00:00
|
|
|
|
| int_id | int_nullable | float | float_nullable |
|
2020-04-08 14:22:25 +00:00
|
|
|
|
+------+----------+-----+----------+
|
2020-04-03 13:23:32 +00:00
|
|
|
|
| 1 | NULL | 2 | NULL |
|
2020-04-08 14:22:25 +00:00
|
|
|
|
+------+----------+-----+----------+
|
2020-04-03 13:23:32 +00:00
|
|
|
|
1 row in set (0,00 sec)
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
从ClickHouse中查询数据:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT * FROM mysql('localhost:3306', 'test', 'test', 'bayonet', '123')
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
``` text
|
|
|
|
|
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
|
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │ 2 │ ᴺᵁᴸᴸ │
|
|
|
|
|
└────────┴──────────────┴───────┴────────────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-12 09:50:45 +00:00
|
|
|
|
替换和插入:
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
INSERT INTO FUNCTION mysql('localhost:3306', 'test', 'test', 'bayonet', '123', 1) (int_id, float) VALUES (1, 3);
|
|
|
|
|
INSERT INTO TABLE FUNCTION mysql('localhost:3306', 'test', 'test', 'bayonet', '123', 0, 'UPDATE int_id = int_id + 1') (int_id, float) VALUES (1, 4);
|
|
|
|
|
SELECT * FROM mysql('localhost:3306', 'test', 'test', 'bayonet', '123');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```text
|
|
|
|
|
┌─int_id─┬─float─┐
|
|
|
|
|
│ 1 │ 3 │
|
|
|
|
|
│ 2 │ 4 │
|
|
|
|
|
└────────┴───────┘
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## 另请参阅 {#see-also}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-30 18:19:18 +00:00
|
|
|
|
- [该 ‘MySQL’ 表引擎](../../engines/table-engines/integrations/mysql.md)
|
|
|
|
|
- [使用MySQL作为外部字典的来源](../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-sources.md#dicts-external_dicts_dict_sources-mysql)
|