我有一张EAV表。我想在单个查询中将其转换为普通表。我怎样才能做到这一点? 表A
| ID | Entity | Attribute |
+----+--------+-----------+
| 1 | AA | 5 |
| 2 | AA | 2 |
| 3 | AA | 4 |
| 1 | BB | 6 |
| 2 | BB | 5 |
表B(预期。目前只有ID)
| ID | AA | BB |
+----+-----+------+
| 1 | 5 | 6 |
| 2 | 2 | 5 |
| 3 | 4 | NULL |
我尝试了以下内容:
update B join A using(ID) set b.AA=(case when Entity='AA' then Attribute end),b.BB=(case when Entity='BB' then Attribute end);
update B join A using(ID) set b.AA=(case when Entity='AA' then Attribute else AA end),b.BB=(case when Entity='BB' then Attribute else BB end);
但表格没有得到更新。对于所有ID,这两个字段都设置为NULL。我可以为每个实体编写单独的更新查询,但我有> 100个不同的实体,每个查询需要几个小时才能运行(我有很多记录)。