MySQL table 到 csv 提取 - 基于列值 - 转换为行值

MySQL table to csv extract- based on a column value -convert to row value

我正在尝试通过连接两个 MySQL 表来提取 csv。

下面是表格。

表 1:

account_no  transaction amount  txn_date
------------------------------------------
123         Withdraw    100     2021-01-01
111         NEFT        150     2021-02-01
321         IMPS        200     2021-10-25
222         RTGS        250     2021-12-31

表 2:

Account_no  Branch
-----------------------
123             a
111             b
321             c
222             d

想用 headers,

提取 csv

Account_no,txn_date,分支机构,Amount-Withdraw,Amount-NEFT,Amount-IMPS,Amount-RTGS

我试过了:

select T1.Account_no,T1.txn_date,T2.Branch,
case when T1.transaction = 'Withdraw' then amount  end as'Amount-Withdraw'
case when T1.transaction= 'NEFT' then amount  end as 'Amount-NEFT'
case when T1.transaction='IMPS' then Amount  end as 'Amount-IMPS'
case when T1.transaction= 'RTGS' then Amount end  as ' Amount-RTGS'
from Table1 T1 Join Table2 T2 on T1.Account_no=T2.Account_no;

但它没有按预期工作。

需要输出:

Account_no,txn_date,Branch,Amount-Withdraw,Amount-NEFT,Amount-IMPS,Amount-RTGS
123,2021-01-01,a,100,0,0,0
111,2021-01-01,b,0,150,0,0
321,2021-01-01,c,0,0,200,0
......

试试这个

select T1.account_no,T1.txn_date,T2.Branch,
case when T1.`transaction` = 'Withdraw' then amount  end as'Amount-Withdraw',
case when T1.`transaction`= 'NEFT' then amount  end as 'Amount-NEFT',
case when T1.`transaction`='IMPS' then Amount  end as 'Amount-IMPS',
case when T1.`transaction`= 'RTGS' then Amount end  as ' Amount-RTGS'
FROM t1 T1, t2 T2 where T1.account_no=T2.account_no;