MySQL:如何转换为EAV?

发布于 2024-11-28 21:43:54 字数 754 浏览 3 评论 0原文

假设我有下表:

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 |
-----------------------------------

注意:

  • two.fk_id 值来自 one.id
  • 二.attribute 值来自 one.first_name
  • 二.value 值来自 one.last_name

我需要创建表表一中的两个,这样我就可以运行一些测试。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-12-05 21:43:54

我假设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

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
痴情换悲伤 2024-12-05 21:43:54

看在上帝的份上,不要使用 EAV!!!!
如果您需要,请改用 NoSQL 数据库!

For the love of god, don't use EAV!!!!
If you need that, use a NoSQL database instead!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文