如何更改 mysql 中导入电子表格的数据类型?

发布于 2024-10-03 09:05:20 字数 394 浏览 1 评论 0原文

我已将美国政府统计数据的电子表格导入到 mysql 表中,目的是完成教程。

然而,美国政府似乎改变了格式,将一些数字用引号括起来,将它们变成字符串。

例如,这是我应该下载的制表符分隔数据的格式:

CN010010 01 001 "Autauga County, AL" 2005 23831 23061 770 3.2

但是,它看起来像这样: CN010010 01 001 "Autauga County, AL" 2005 "23,831 " "23,061 " 770 3.2

结果,我想要作为整数导入的两个关键数据列(23831 和 23061 位)注册为 0 - 可能是因为它不符合数据类型。

现在和将来解决这个问题的最佳解决方案是什么?

提前致谢。

I've imported a spreadsheet of U.S. Government statistics into a mysql table with the aim of working through a tutorial.

However, it would appear that the U.S. Government has changed its formatting so that some of numerical figures are wrapped in quote marks, turning them into strings.

For example, this is the format of the tab-separated data that I'm supposed to have downloaded:

CN010010 01 001 "Autauga County, AL" 2005 23831 23061 770 3.2

However, it looks like this: CN010010 01 001 "Autauga County, AL" 2005 "23,831 " "23,061 " 770 3.2

As a result the two key columns of data (the 23831 and 23061 bits) that I want to import as integers are registering as 0 - presumably because it doesn't meet the data type.

What's the best solution for resolving this problem, now and in the future?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-10-10 09:05:20

“逗号”可能会引起问题。您是否为此使用加载数据文件?

假设表定义:

CREATE TABLE `t` (
  `code` varchar(255) DEFAULT NULL,
  `state` char(2) DEFAULT NULL,
  `city` char(3) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `year` int(11) DEFAULT NULL,
  `pop1` int(11) DEFAULT NULL,
  `pop2` int(11) DEFAULT NULL,
  `diff` int(11) DEFAULT NULL,
  `ratio` float DEFAULT NULL
)

以下将导入文件:

load data local infile "test.txt"
into table t
columns terminated by '\t'
optionally enclosed by '"'
(code, state, city, name, year, @var1, @var2, diff, ratio)
set pop1=replace(@var1,",", ""),
    pop2=replace(@var2, ",", "");

将插入以下行:

 code: CN010010
state: 01
 city: 001
 name: Autauga County, AL
 year: 2005
 pop1: 23831
 pop2: 23061
 diff: 770
ratio: 3.2

The "comma" might cause problem. Are you using load data infile for this?

Assuming table definition:

CREATE TABLE `t` (
  `code` varchar(255) DEFAULT NULL,
  `state` char(2) DEFAULT NULL,
  `city` char(3) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `year` int(11) DEFAULT NULL,
  `pop1` int(11) DEFAULT NULL,
  `pop2` int(11) DEFAULT NULL,
  `diff` int(11) DEFAULT NULL,
  `ratio` float DEFAULT NULL
)

The following would import the file:

load data local infile "test.txt"
into table t
columns terminated by '\t'
optionally enclosed by '"'
(code, state, city, name, year, @var1, @var2, diff, ratio)
set pop1=replace(@var1,",", ""),
    pop2=replace(@var2, ",", "");

would insert the following row:

 code: CN010010
state: 01
 city: 001
 name: Autauga County, AL
 year: 2005
 pop1: 23831
 pop2: 23061
 diff: 770
ratio: 3.2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文