【问题标题】:MySQL table to csv extract- based on a column value -convert to row valueMySQL 表到 csv 提取 - 基于列值 - 转换为行值
【发布时间】:2021-09-22 18:00:22
【问题描述】:

我正在尝试通过连接两个 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

想提取带有标题的csv,

Account_no,txn_date,Branch,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
......

【问题讨论】:

    标签: mysql export-to-csv


    【解决方案1】:

    试试这个

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2021-10-30
      相关资源
      最近更新 更多