MySQL 中是否有像 PostgreSQL 一样的数组数据类型?

发布于 2024-10-30 18:39:26 字数 223 浏览 0 评论 0原文

我需要在 MySQL 数据库中存储整数数组。 MySQL 中有类似的东西吗?

 CREATE TABLE tictactoe (
    squares   integer[3][3]
);

我想存储尺寸为 20x6 的矩阵。我不想创建一个包含 120 列的表。不需要对该字段进行查询,只需要存储和检索完整的矩阵。

如果重要的话,我使用 Perl。

I need to store arrays of integers in a MySQL database. In there something equivalent to this in MySQL?

 CREATE TABLE tictactoe (
    squares   integer[3][3]
);

I want to store matrices with dimension 20x6. I don't feel like creating a table with 120 columns. There is no need to query on this field, just need to store and retrieve full matrices.

If it matters, i use Perl.

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

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

发布评论

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

评论(4

痴梦一场 2024-11-06 18:39:26

不,没有这样的事情。有一个开放工作日志,但在实施方面尚未取得任何进展这个功能。

您必须以某种方式模拟这一点,使用多个字段(在您的例子中为 9 个)或将整数打包成更大的数据类型(例如 blob)。

No, there is no such thing. There is an open worklog for that, but no progress has been made on implementing this feature.

You have to emulate this somehow, using either multiple fields (9 in your case) or pack the integers together into a larger datatype (blob for example).

落花随流水 2024-11-06 18:39:26

使用适当的分隔符以文本格式存储它们。
例如:- 如果要存储 1 到 9 的数字,请将它们以文本格式存储为 1-2-3-4-5-6-7-8-9,其中“-”是分隔符。分解字符串并得到所需的数字。

Store them in a text format using proper delimiters.
ex:- If you want to store numbers from 1 to 9, store them in text format as 1-2-3-4-5-6-7-8-9 where '-' is a delimiter. Explode the string and get the desired numbers.

清音悠歌 2024-11-06 18:39:26

MySQL 中没有数组数据类型。但是您可以以 JSON 数组形式存储数组。当然,在这种情况下 SQL 文本会更复杂。

示例:

-- create JSON column which will store an array
CREATE TABLE test (id INT, value JSON);
-- insert 2-dimension array
INSERT INTO test (id, value) VALUES 
  (1, '[[1,2,3], [4,5,6], [7,8,9]]'),
  (11, '[[11,12,13], [14,15,16], [17,18,19]]');
-- see the data
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(value AS CHAR)
1[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
11[[11, 12, 13], [14, 15 , 16], [17, 18, 19]]
-- retrieve the values for an element array(1,2) which are 6 and 16
-- remember that the elements enumeration starts from zero
SELECT id, value->'$[1][2]' AS `array(1,2)` FROM test;
id数组(1,2)
16
1116
-- update the value for this element to 10 for the row with id=1
UPDATE test
SET value = JSON_REPLACE(value, '$[1][2]', 10)
WHERE id = 1;
-- view the result
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(值 AS CHAR)
1[[1, 2, 3], [4, 5, 10], [7, 8, 9]]
11[[11, 12, 13], [14, 15, 16], [17, 18, 19] ]]
-- update the value for the whole row to [20,21,22] for the row with id=11
UPDATE test
SET value = JSON_REPLACE(value, '$[1]', JSON_ARRAY(20,21,22))
WHERE id = 11;
-- view the result
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(值 AS CHAR)
1[[1, 2, 3], [4, 5, 10], [7, 8, 9]]
11[[11, 12, 13], [20, 21, 22], [17, 18, 19]
-- retrieve the whole array as a table
SELECT test.id, jsontable.num, jsontable.`1`, jsontable.`2`, jsontable.`3`
FROM test
CROSS JOIN JSON_TABLE(test.value, 
                      '$[*]' COLUMNS (num FOR ORDINALITY,
                                      `1` INT PATH '$[0]',
                                      `2` INT PATH '$[1]',
                                      `3` INT PATH '$[2]')) jsontable;
编号1]23
11123
124510
13789
111111213
112202122
113171819

小提琴 JSON 是二进制数据类型,需要 CAST(value AS CHAR) 将其转换为字符串。

There is no array datatype in MySQL. But you may store array in JSON array form. Of course the SQL text will be more complex in this case.

A sample:

-- create JSON column which will store an array
CREATE TABLE test (id INT, value JSON);
-- insert 2-dimension array
INSERT INTO test (id, value) VALUES 
  (1, '[[1,2,3], [4,5,6], [7,8,9]]'),
  (11, '[[11,12,13], [14,15,16], [17,18,19]]');
-- see the data
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(value AS CHAR)
1[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
11[[11, 12, 13], [14, 15, 16], [17, 18, 19]]
-- retrieve the values for an element array(1,2) which are 6 and 16
-- remember that the elements enumeration starts from zero
SELECT id, value->'$[1][2]' AS `array(1,2)` FROM test;
idarray(1,2)
16
1116
-- update the value for this element to 10 for the row with id=1
UPDATE test
SET value = JSON_REPLACE(value, '$[1][2]', 10)
WHERE id = 1;
-- view the result
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(value AS CHAR)
1[[1, 2, 3], [4, 5, 10], [7, 8, 9]]
11[[11, 12, 13], [14, 15, 16], [17, 18, 19]]
-- update the value for the whole row to [20,21,22] for the row with id=11
UPDATE test
SET value = JSON_REPLACE(value, '$[1]', JSON_ARRAY(20,21,22))
WHERE id = 11;
-- view the result
SELECT id, CAST(value AS CHAR) FROM test;
idCAST(value AS CHAR)
1[[1, 2, 3], [4, 5, 10], [7, 8, 9]]
11[[11, 12, 13], [20, 21, 22], [17, 18, 19]]
-- retrieve the whole array as a table
SELECT test.id, jsontable.num, jsontable.`1`, jsontable.`2`, jsontable.`3`
FROM test
CROSS JOIN JSON_TABLE(test.value, 
                      '$[*]' COLUMNS (num FOR ORDINALITY,
                                      `1` INT PATH '$[0]',
                                      `2` INT PATH '$[1]',
                                      `3` INT PATH '$[2]')) jsontable;
idnum123
11123
124510
13789
111111213
112202122
113171819

fiddle

PS. JSON is binary datatype, CAST(value AS CHAR) needed for to convert it to string.

抚笙 2024-11-06 18:39:26

不,没有。简短的答案,但不知道你做了什么,这就是答案。

No there is not. Short answer but without knowing what you have do that is the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文