【发布时间】:2026-02-03 15:30:01
【问题描述】:
考虑使用 JSON 字段的 MySQL 数据库 (8.x):
mysql> desc users_health;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| user_id | int | NO | | NULL | |
| data | json | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
在这个答案中,有一种方法可以将列导出到 csv 字段:
How to output MySQL query results in CSV format?
我想要实现的是仅将 data 列中的数据导出到 CSV。这些数据组织良好,由键值对组成,如下所示:
{"email": "x@example.com", "user_id": 100, "ivr_used": false, "ivr_enabled": true, "callerids_used": true, "call-queue_used": false ...}
我希望将其导出为 CSV 文件,如下所示:
+--------------+-----------+----------+-------------+----------+
| email | user_id | ivr_used | ivr_enabled | ... |
+--------------+----------+-----------+-------------+----------+
| x@example.com| 100 | false | true | ... |
+--------------+----------+-----------+-------------+----------+
| y@example.com| 101 | true | true | ... |
+--------------+----------+-----------+-------------+----------+
....
这甚至可以使用仅 MySQL 的解决方案,还是我必须获取数据并在其他地方处理它?
对我来说部分有效的解决方案是将数据导出到 JSON 文件的可能性。
【问题讨论】:
-
您想要每对 1 个 excel 列?
-
是的,完全正确。一个键是一列。此表中的每条记录在
data列中具有完全相同的结构 -
也许你应该在 CSV 文件中向我们展示你想要的输出示例
标签: mysql json export-to-csv