<tfoot id='WVSfg'></tfoot>
      <i id='WVSfg'><tr id='WVSfg'><dt id='WVSfg'><q id='WVSfg'><span id='WVSfg'><b id='WVSfg'><form id='WVSfg'><ins id='WVSfg'></ins><ul id='WVSfg'></ul><sub id='WVSfg'></sub></form><legend id='WVSfg'></legend><bdo id='WVSfg'><pre id='WVSfg'><center id='WVSfg'></center></pre></bdo></b><th id='WVSfg'></th></span></q></dt></tr></i><div id='WVSfg'><tfoot id='WVSfg'></tfoot><dl id='WVSfg'><fieldset id='WVSfg'></fieldset></dl></div>

      <small id='WVSfg'></small><noframes id='WVSfg'>

        <bdo id='WVSfg'></bdo><ul id='WVSfg'></ul>
    1. <legend id='WVSfg'><style id='WVSfg'><dir id='WVSfg'><q id='WVSfg'></q></dir></style></legend>

        MySQL 从 CSV 数据加载 NULL 值

        时间:2023-08-17
        <legend id='DRAWt'><style id='DRAWt'><dir id='DRAWt'><q id='DRAWt'></q></dir></style></legend>

        <small id='DRAWt'></small><noframes id='DRAWt'>

          • <bdo id='DRAWt'></bdo><ul id='DRAWt'></ul>
                <tbody id='DRAWt'></tbody>
              <tfoot id='DRAWt'></tfoot>

                  <i id='DRAWt'><tr id='DRAWt'><dt id='DRAWt'><q id='DRAWt'><span id='DRAWt'><b id='DRAWt'><form id='DRAWt'><ins id='DRAWt'></ins><ul id='DRAWt'></ul><sub id='DRAWt'></sub></form><legend id='DRAWt'></legend><bdo id='DRAWt'><pre id='DRAWt'><center id='DRAWt'></center></pre></bdo></b><th id='DRAWt'></th></span></q></dt></tr></i><div id='DRAWt'><tfoot id='DRAWt'></tfoot><dl id='DRAWt'><fieldset id='DRAWt'></fieldset></dl></div>
                  本文介绍了MySQL 从 CSV 数据加载 NULL 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个文件,可以包含 3 到 4 列用逗号分隔的数值.空字段被定义为例外,当它们位于行的末尾时:

                  1,2,3,4,51,2,3,,51,2,3

                  下表是在 MySQL 中创建的:

                  <前>+-------+--------+------+-------+-------+-------+|领域 |类型 |空 |钥匙 |默认 |额外 |+-------+--------+------+-------+-------+-------+|一 |整数(1) |是 ||空 |||二 |整数(1) |是 ||空 |||三 |整数(1) |是 ||空 |||四 |整数(1) |是 ||空 |||五 |整数(1) |是 ||空 ||+-------+--------+------+-------+-------+-------+

                  我正在尝试使用 MySQL LOAD 命令加载数据:

                  LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS终止于 "," 行终止于 "\n";

                  结果表:

                  <前>+------+------+-------+------+------+|一 |二 |三 |四 |五 |+------+------+-------+------+------+|1 |2 |3 |4 |5 ||1 |2 |3 |0 |5 ||1 |2 |3 |空 |空 |+------+------+-------+------+------+

                  问题在于,当原始数据中的字段为空且未定义时,MySQL 出于某种原因不使用列的默认值(即 NULL)而使用零.当字段完全丢失时,NULL 被正确使用.

                  不幸的是,在此阶段我必须能够区分 NULL 和 0,因此我们将不胜感激.

                  谢谢

                  编辑

                  显示警告的输出:

                  <前>+---------+------+--------------------------------------------------------+|级别 |代码 |留言 |+---------+------+--------------------------------------------------------+|警告 |第1366章不正确的整数值:第 2 行的列 'four' 的 '' ||警告 |第1261章第 3 行不包含所有列的数据 ||警告 |第1261章第 3 行不包含所有列的数据 |+---------+------+--------------------------------------------------------+

                  解决方案

                  这将满足您的需求.它将第四个字段读入局部变量,然后将实际字段值设置为 NULL,如果局部变量最终包含一个空字符串:

                  LOAD DATA INFILE '/tmp/testdata.txt'INTO TABLE MOO以,"结尾的字段以\n"结尾的行(一,二,三,@vfour,五)SET 四 = NULLIF(@vfour,'');

                  如果它们都可能为空,那么您可以将它们全部读入变量并有多个 SET 语句,如下所示:

                  LOAD DATA INFILE '/tmp/testdata.txt'INTO TABLE MOO以,"结尾的字段以\n"结尾的行(@vone、@vtwo、@vthree、@vfour、@vfive)放一 = NULLIF(@vone,''),二 = NULLIF(@vtwo,''),三 = NULLIF(@vthree,''),四 = NULLIF(@vfour,'');

                  I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row:

                  1,2,3,4,5
                  1,2,3,,5
                  1,2,3
                  

                  The following table was created in MySQL:

                  +-------+--------+------+-----+---------+-------+
                  | Field | Type   | Null | Key | Default | Extra |
                  +-------+--------+------+-----+---------+-------+
                  | one   | int(1) | YES  |     | NULL    |       | 
                  | two   | int(1) | YES  |     | NULL    |       | 
                  | three | int(1) | YES  |     | NULL    |       | 
                  | four  | int(1) | YES  |     | NULL    |       | 
                  | five  | int(1) | YES  |     | NULL    |       | 
                  +-------+--------+------+-----+---------+-------+
                  

                  I am trying to load the data using MySQL LOAD command:

                  LOAD DATA INFILE '/tmp/testdata.txt' INTO TABLE moo FIELDS 
                  TERMINATED BY "," LINES TERMINATED BY "\n";
                  

                  The resulting table:

                  +------+------+-------+------+------+
                  | one  | two  | three | four | five |
                  +------+------+-------+------+------+
                  |    1 |    2 |     3 |    4 |    5 | 
                  |    1 |    2 |     3 |    0 |    5 | 
                  |    1 |    2 |     3 | NULL | NULL | 
                  +------+------+-------+------+------+
                  

                  The problem lies with the fact that when a field is empty in the raw data and is not defined, MySQL for some reason does not use the columns default value (which is NULL) and uses zero. NULL is used correctly when the field is missing alltogether.

                  Unfortunately, I have to be able to distinguish between NULL and 0 at this stage so any help would be appreciated.

                  Thanks S.

                  edit

                  The output of SHOW WARNINGS:

                  +---------+------+--------------------------------------------------------+
                  | Level   | Code | Message                                                |
                  +---------+------+--------------------------------------------------------+
                  | Warning | 1366 | Incorrect integer value: '' for column 'four' at row 2 | 
                  | Warning | 1261 | Row 3 doesn't contain data for all columns             | 
                  | Warning | 1261 | Row 3 doesn't contain data for all columns             | 
                  +---------+------+--------------------------------------------------------+
                  

                  解决方案

                  This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string:

                  LOAD DATA INFILE '/tmp/testdata.txt'
                  INTO TABLE moo
                  FIELDS TERMINATED BY ","
                  LINES TERMINATED BY "\n"
                  (one, two, three, @vfour, five)
                  SET four = NULLIF(@vfour,'')
                  ;
                  

                  If they're all possibly empty, then you'd read them all into variables and have multiple SET statements, like this:

                  LOAD DATA INFILE '/tmp/testdata.txt'
                  INTO TABLE moo
                  FIELDS TERMINATED BY ","
                  LINES TERMINATED BY "\n"
                  (@vone, @vtwo, @vthree, @vfour, @vfive)
                  SET
                  one = NULLIF(@vone,''),
                  two = NULLIF(@vtwo,''),
                  three = NULLIF(@vthree,''),
                  four = NULLIF(@vfour,'')
                  ;
                  

                  这篇关于MySQL 从 CSV 数据加载 NULL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL 中的类型:BigInt(20) 与 Int(20) 下一篇:用户“root@localhost"的访问被拒绝(使用密码:

                  相关文章

                  最新文章

                  <tfoot id='cJ8ie'></tfoot>
                  <legend id='cJ8ie'><style id='cJ8ie'><dir id='cJ8ie'><q id='cJ8ie'></q></dir></style></legend>

                  <small id='cJ8ie'></small><noframes id='cJ8ie'>

                      • <bdo id='cJ8ie'></bdo><ul id='cJ8ie'></ul>
                    1. <i id='cJ8ie'><tr id='cJ8ie'><dt id='cJ8ie'><q id='cJ8ie'><span id='cJ8ie'><b id='cJ8ie'><form id='cJ8ie'><ins id='cJ8ie'></ins><ul id='cJ8ie'></ul><sub id='cJ8ie'></sub></form><legend id='cJ8ie'></legend><bdo id='cJ8ie'><pre id='cJ8ie'><center id='cJ8ie'></center></pre></bdo></b><th id='cJ8ie'></th></span></q></dt></tr></i><div id='cJ8ie'><tfoot id='cJ8ie'></tfoot><dl id='cJ8ie'><fieldset id='cJ8ie'></fieldset></dl></div>