MySQL:如何转换为 EAV - 第 2 部分?
这是第 1 部分: MySQL:如何转换为 EAV?
现在我想也做一些不同的事情。假设我有下表:
TABLE: one
=======================================
| id | fk_id | attribute | value |
=======================================
| 1 | 10 | first_name | John |
| 2 | 10 | last_name | Doe |
| 3 | 55 | first_name | Bob |
| 4 | 55 | last_name | Smith |
---------------------------------------
我想将其转换为此 EAV 模型:
TABLE: attribute
===================
| id | attribute |
===================
| 1 | first_name |
| 2 | last_name |
-------------------
TABLE: value
=====================================
| id | attribute_id | fk_id | value |
=====================================
| 1 | 1 | 10 | John |
| 2 | 2 | 10 | Doe |
| 3 | 1 | 55 | Bob |
| 4 | 2 | 55 | Smith |
-------------------------------------
假设表 attribute
和 value
已定义。如何将表 one
中的数据插入到两个目标表中。对我来说一个大问题是如何获得正确的关系 (attribute.id
=> value.attribute_id
)。
Here's Part 1: MySQL: how to convert to EAV?
Now I want to also do something different. Say I have the following table:
TABLE: one
=======================================
| id | fk_id | attribute | value |
=======================================
| 1 | 10 | first_name | John |
| 2 | 10 | last_name | Doe |
| 3 | 55 | first_name | Bob |
| 4 | 55 | last_name | Smith |
---------------------------------------
I want to convert it to this EAV model:
TABLE: attribute
===================
| id | attribute |
===================
| 1 | first_name |
| 2 | last_name |
-------------------
TABLE: value
=====================================
| id | attribute_id | fk_id | value |
=====================================
| 1 | 1 | 10 | John |
| 2 | 2 | 10 | Doe |
| 3 | 1 | 55 | Bob |
| 4 | 2 | 55 | Smith |
-------------------------------------
Assume the tables attribute
and value
are already defined. How do I insert the data from table one
into the two target tables. One big problem for me is how to get the relationship (attribute.id
=> value.attribute_id
) right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)