本文介绍了MySQL:如何转换为EAV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下表格:
TABLE: one
===============================
| id | first_name | last_name |
===============================
| 1 | John | Doe |
| 2 | Jane | Smith |
-------------------------------
我想将它转换为EAV:
I want to convert it to an 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 |
-----------------------------------
注意:
- two.fk_id值来自一个。 id
- two.attribute值来自one.first_name
- two.value值来自one.last_name
我需要从表一创建表二,所以我可以运行一些测试。
I need to create table two from table one so I can run some tests.
推荐答案
我假设two.id是一个自动递增的整数,所以我将它留给目标表来分配这些值。
I'm assuming two.id is an autoincrementing integer, so I'll leave it to the target table to assign those values.
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:如何转换为EAV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!