Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据另一字段中的值将一个字段拆分为多列_Mysql_Sql - Fatal编程技术网

Mysql 根据另一字段中的值将一个字段拆分为多列

Mysql 根据另一字段中的值将一个字段拆分为多列,mysql,sql,Mysql,Sql,我的数据存储在mysql中 我的表格数据如下 3 credit 500.00 3 debit 500.00 4 credit 300.00 4 debit 300.00 5 credit 300.00 5 debit 300.00 6 credit 300.00 6 debit 300.00 我想写一个查询,将数据写为 客户id、贷方、借方、最终金额 其中,作为最终金额=贷记借方 有人能帮我查询吗?您可以使用条件聚合: select cu

我的数据存储在mysql中

我的表格数据如下

3   credit  500.00
3   debit   500.00
4   credit  300.00
4   debit   300.00
5   credit  300.00
5   debit   300.00
6   credit  300.00
6   debit   300.00
我想写一个查询,将数据写为 客户id、贷方、借方、最终金额

其中,作为最终金额=贷记借方


有人能帮我查询吗?

您可以使用条件聚合:

select customerid, 
       sum(case when type = 'credit' then amount else 0 end) as credit,
       sum(case when type = 'debit' then amount else 0 end) as debit,
       sum(case when type = 'credit' then amount
                when type = 'debit' then - amount
                else 0
           end) as credit
from t
group by customerid;

您可以使用条件聚合:

select customerid, 
       sum(case when type = 'credit' then amount else 0 end) as credit,
       sum(case when type = 'debit' then amount else 0 end) as debit,
       sum(case when type = 'credit' then amount
                when type = 'debit' then - amount
                else 0
           end) as credit
from t
group by customerid;

继续。试试看。欢迎来到堆栈溢出!我们是一个志愿者社区,为陷入特定任务的程序员提供帮助。我们不是为您编写免费代码的服务机构。请阅读,我知道我必须使用内部连接。不知怎么搞不清楚该怎么做。或者像往常一样,坐着等GL为你做一切。@草莓戈登需要这些分数。除非它实际上是由堆栈溢出创建的AI程序。继续。试试看。欢迎来到堆栈溢出!我们是一个志愿者社区,为陷入特定任务的程序员提供帮助。我们不是为您编写免费代码的服务机构。请阅读,我知道我必须使用内部连接。不知怎么搞不清楚该怎么做。或者像往常一样,坐着等GL为你做一切。@草莓戈登需要这些分数。除非它实际上是由堆栈溢出创建的AI程序。