# Mapping

mapping 前缀用于创建映射表，例如，此映射表在脚本运行期间可用于替换字段值和字段名。

语法：

Mapping( loadstatement | selectstatement )

该 mapping 前缀可置于 LOAD 或 SELECT 语句之前，并将正在加载的语句的结果存储为映射表。映射提供了一种有效的途径在脚本执行过程中替代字段值，例如：将 US、U.S. 或替换为 USA。映射表必须有两个字段，第一个字段包含比较值，第二个字段包含所需的映射值。映射表暂时存储在内存中，执行脚本后将自动删除。

使用 Map … Using 语句、Rename Field 语句、Applymap() 函数或 Mapsubstring() 函数可访问映射表的内容。

示例：

在此例中，我们加载了销售人员和国家代码（表示销售人员所居住的国家）的列表。我们使用表格将国家代码映射到国家，以便将国家代码替换为国家名称。在映射表中仅定义了三个国家，其他国家代码已映射到'Rest of the world'。

```
// Load mapping table of country codes:map1:mapping LOAD * Inline [CCode, CountrySw, SwedenDk, DenmarkNo, Norway] ; // Load list of salesmen, mapping country code to country
```

```
 // If the country code is not in the mapping table, put Rest of the worldSalespersons:LOAD *, ApplyMap('map1', CCode,'Rest of the world') As CountryInline [CCode, SalespersonSw, JohnSw, Mary
```

```
Sw, PerDk, PrebenDk, OlleNo, OleSf, Risttu] ;// We don't need the CCode anymoreDrop Field 'CCode';
```

最终生成的表格如下所示：

Example 1

| Salesperson | Country |
| --- | --- |
| John | Sweden |
| Mary | Sweden |
| Per | Sweden |
| Preben | Denmark |
| Olle | Denmark |
| Ole | Norway |
| Risttu | Rest of the world |

## 了解详情

* [Load](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptRegularStatements/Load.htm)
* [Select](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptRegularStatements/Select.htm)
* [Map](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptRegularStatements/Map.htm)
* [Unmap](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/ScriptRegularStatements/Unmap.htm)
* [ApplyMap - 脚本函数](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/MappingFunctions/ApplyMap.htm)
* [MapSubstring - 脚本函数](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/MappingFunctions/MapSubstring.htm)
* [映射函数](https://help.qlik.com/zh-CN/qlikview/May2024/Subsystems/Client/Content/QV_QlikView/Scripting/MappingFunctions/mapping-functions.htm)