mysql eav,MySQL:如何转换为EAV?

Say I have the following table:

TABLE: one

===============================

| id | first_name | last_name |

===============================

| 1 | John | Doe |

| 2 | Jane | Smith |

-------------------------------

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 |

-----------------------------------

Note:

two.fk_id values came from one.id

two.attribute values came from one.first_name

two.value values came from one.last_name

I need to create table two from table one so I can run some tests.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值