假设我有下表:
TABLE: one
===============================
| id | first_name | last_name |
===============================
| 1 | John | Doe |
| 2 | Jane | Smith |
-------------------------------
我想把它转换成EAV:
TABLE: two
===================================
| id | fk_id | attribute | value |
===================================
| 1 | 1 | first_name | John |
| 2 | 1 | last_name | Doe |
| 3 | 2 | first_name | Jane |
| 4 | 2 | last_name | Smith |
-----------------------------------
注:
两个.fk_id值来自一个.id
2.属性值来自one.first_name
2.值来自1.姓氏
我需要从表1创建表2以便运行一些测试。
最佳答案
我假设two.id是一个自动递增的整数,所以我将把它留给目标表来分配这些值。
select id as fk_id, 'first_name' as attribute, first_name as value
from one
union all
select id as fk_id, 'last_name', last_name
from one
order by fk_id, attribute
关于mysql - MySQL:如何转换为EAV?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7020661/