mysql表数据根据数据类型在单行中输出

标签 mysql sql

我在名为 wallet_txns 的 mysql 表中有以下数据。

wlt_name   wlt_txn_type   wlt_txn_amount
A           Income              200
A           Expense             100 
B           Income              100
B           Income              500
B           Expense             200

我试图获得如下数据的输出(单行中的收入和支出之和)

Wlt_name    Expense       Income
A           100            200
B           200            600

我使用了以下查询,但我没有得到预期的输出,(收入和支出在单独的行中)请帮助...

select
    wlt_name,
    if(wlt_txn_type = 'Expense', wlt_txn_amount, 0) as Expense,
    if(wlt_txn_type = 'Income', wlt_txn_amount, 0) as Income
from wallet_txns
;

最佳答案

也许您可以帮助这部分代码:SUM(IF(wlt_txn_type = "Income", wlt_txn_amount, 0)) AS IncomeTotal

关于mysql表数据根据数据类型在单行中输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30525119/

相关文章:

mysql - 在数据库中搜索超过 300 万条。条目

mysql - SQL 在我的用户表上每个季度+年的增长

mysql - SQL UNION 语句位于彼此之下

c# - SQL 表参数

php - 使用ajax发送变量

python - Django 迁移到 MySql

android - 将多个sqlite数据库同步到一个mysql数据库

mysql - 合并 SQL 中相似 ID 的行?

python - **SQLAlchemy** :Get a count of one column where another column is distinct

sql - 更新 SQL MS Access 2010