mysql - 将一个表的行值附加到另一个表的列

标签 mysql rows

我有一个表 T1,其中包含姓名、电子邮件、电话号码等标准列。自定义字段作为键、值存储在 custom_fields 表中。

我需要将 custom_fields 的键名称作为实际表上的列名称。我怎样才能实现这个目标

表T1

ID 姓名 电子邮件

1 约翰 john@example.com

2 萨姆 Same@mydomain.com

自定义字段

id T1_id键值

1 1 32 岁

2 1 职位名称 CEO

3 2 40 岁

4 2 辆车拥有福特 EcoSport

必填

T1_id 姓名 电子邮件 年龄 职位_职位 汽车_拥有

1 John john@example.com 32 CEO 未指定

2 Sam Same@mydomain.com 40 未指定福特 EcoSport

最佳答案

您应该使用表 custom_fields 两次,一次用于工作,一次用于汽车

select  t1.id, t1.name, t1.email, t2.value, t3.value 
from  t1 
inner join custom_fields t2 on t1.id = t2.t1_id and  t2.key = 'Job Title' 
inner join custom_fields t3 on t1.id = t3.t1_id and  t3.key = 'Car Owned' 

关于mysql - 将一个表的行值附加到另一个表的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56739736/

相关文章:

mysql - 查找两行之间的最大差异

HTML/CSS 隐藏表行(不工作)

MySQL 创建表时出错

javascript - 如果数据库中有新条目,聊天更新

php - MYSQL/Laravel : convert a WHERE into a variable

Excel 在行内排列列

Mysql根据条件选择值

php - 删除插入到 mysql 中的 post html、js、css

sql - 在 SQL 中将列值重复到行中

c# - 批量插入未按预期工作