将具有 3 个表的旧 MySQL 迁移到 JSON
Migrate Old MySQL with 3 tables to JSON
我正在努力将我的 PHP/MySQL 数据迁移到 Firebase,它只允许通过 JSON 导入。我在几年前制作了这 5 MySQL 个表格,但没有正确完成。我从来没有正式将任何东西设置为外键,所以我使用单独的查询而不是连接。
现在,我正在尝试编写一个脚本来移动大约 4000 个组合行。我正在使用 PHPMyAdmin 并在那里提供所有标准功能。我真的很感激任何正确方向的提示或提示。提前致谢! :)
这是我的布局的简化版本,包含所有必要的部分:
TABLE users
- id (int, primary key)
- username (varchar)
TABLE posts
- id (int, primary key)
- content (varchar)
- userid (int, should be foreign key to users)
TABLE comments
- id (int, primary key)
- content (varchar)
- userid (int, should be foreign key to users)
- postid (int, should be foreign key to posts)
TABLE commentLikes
- id (int, primary key)
- likeValue (int)
- commentid (int, should be foreign key to comments)
- userid (int, should be foreign key to users)
TABLE postLikes
- id (int, primary key)
- likeValue (int)
- postid (int, should be foreign key to posts)
- userid (int, should be foreign key to users)
基本上,站点上的用户可以创建帖子和对帖子发表评论。评论和帖子都可以点赞。
我绘制了我希望 JSON 文档的样子。我会为用户提供一种类型,为帖子提供一种类型。
Post JSON format
{
id: 100,
content: "A post by user 1",
userid: 1,
comments: [
{
content: "A comment by user 2",
userid: 2,
commentLikes: [
{
likeValue: 1,
userid: 2
},
{
likeValue: -1,
userid: 3
}
]
},
{
content: "A comment by user 3",
userid: 3,
commentsLikes: []
}
],
postLikes: [
{
likeValue: 1,
userid: 2
},
{
likeValue: -1,
userid: 3
}
]
}
User JSON Format
{
id: 1,
username: "Bob"
}
有没有人对如何完成这种转变有任何想法?我知道 MySQL 的设置很糟糕。但是,我不能丢失这些数据。这是我写的一个脚本,但我不相信它有效。即使在脚本运行之后,我如何批量创建这些 JSON 文件以导入到 Firebase 中?
SELECT p.*, c.content, c.userid, cL.likeValue, cL.userid, pL.likeValue, pL.userid
FROM posts p, comments c, commmentLikes cL, postLikes pL
WHERE c.postid = p.id OR pL.postid = p.id
如果您已经阅读到这里,非常感谢您的帮助! :)
使用我安装的 PHPMyadmin 版本 4.2.7.1,我能够直接导出整个 table,或从查询生成的视图 JSON。
如果您的版本没有 JSON 在导出格式选项中列出,只需升级到任何具有 'Export to JSON plugin for PHPMyAdmin' 的版本。看起来是任何版本 >= 3.5.1
我正在努力将我的 PHP/MySQL 数据迁移到 Firebase,它只允许通过 JSON 导入。我在几年前制作了这 5 MySQL 个表格,但没有正确完成。我从来没有正式将任何东西设置为外键,所以我使用单独的查询而不是连接。
现在,我正在尝试编写一个脚本来移动大约 4000 个组合行。我正在使用 PHPMyAdmin 并在那里提供所有标准功能。我真的很感激任何正确方向的提示或提示。提前致谢! :)
这是我的布局的简化版本,包含所有必要的部分:
TABLE users
- id (int, primary key)
- username (varchar)
TABLE posts
- id (int, primary key)
- content (varchar)
- userid (int, should be foreign key to users)
TABLE comments
- id (int, primary key)
- content (varchar)
- userid (int, should be foreign key to users)
- postid (int, should be foreign key to posts)
TABLE commentLikes
- id (int, primary key)
- likeValue (int)
- commentid (int, should be foreign key to comments)
- userid (int, should be foreign key to users)
TABLE postLikes
- id (int, primary key)
- likeValue (int)
- postid (int, should be foreign key to posts)
- userid (int, should be foreign key to users)
基本上,站点上的用户可以创建帖子和对帖子发表评论。评论和帖子都可以点赞。
我绘制了我希望 JSON 文档的样子。我会为用户提供一种类型,为帖子提供一种类型。
Post JSON format
{
id: 100,
content: "A post by user 1",
userid: 1,
comments: [
{
content: "A comment by user 2",
userid: 2,
commentLikes: [
{
likeValue: 1,
userid: 2
},
{
likeValue: -1,
userid: 3
}
]
},
{
content: "A comment by user 3",
userid: 3,
commentsLikes: []
}
],
postLikes: [
{
likeValue: 1,
userid: 2
},
{
likeValue: -1,
userid: 3
}
]
}
User JSON Format
{
id: 1,
username: "Bob"
}
有没有人对如何完成这种转变有任何想法?我知道 MySQL 的设置很糟糕。但是,我不能丢失这些数据。这是我写的一个脚本,但我不相信它有效。即使在脚本运行之后,我如何批量创建这些 JSON 文件以导入到 Firebase 中?
SELECT p.*, c.content, c.userid, cL.likeValue, cL.userid, pL.likeValue, pL.userid
FROM posts p, comments c, commmentLikes cL, postLikes pL
WHERE c.postid = p.id OR pL.postid = p.id
如果您已经阅读到这里,非常感谢您的帮助! :)
使用我安装的 PHPMyadmin 版本 4.2.7.1,我能够直接导出整个 table,或从查询生成的视图 JSON。
如果您的版本没有 JSON 在导出格式选项中列出,只需升级到任何具有 'Export to JSON plugin for PHPMyAdmin' 的版本。看起来是任何版本 >= 3.5.1