如何对 MySQL 中的数据进行非规范化?

How to denormalize the data in MySQL?

我的视图数据如下所示:

数据:

Country  qty   Amt    type
India     42   1000   1
India     32    59    2

我正在尝试根据国家/地区对视图数据进行非规范化,以便两行显示为一行。

所需表格:

 Country  qty   Amt    type  qty    Amt    type
 India     42   1000   1      32    59     2

由于我是 MySQL 的新手,任何人都可以帮我解决同样的问题...

一种方法是 join:

select t1.country, t1.qty, t1.amt, t1.type, t2.qty, t2.amt, t2.type
from t t1 join
     t t2
     on t1.country = t2.country and t1.type = 1 and t2.type = 2;

我假设您要合并的不仅仅是一两行,而是 n 行,如果有印度的,它们应该是一行。如果是这样,您可以简单地使用自连接。

select * from t as t1,t as t2 where t1.country = t2.country