Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Android 为什么使用SQL;sum";返回一个假值_Android_Sql_Sqlite - Fatal编程技术网

Android 为什么使用SQL;sum";返回一个假值

Android 为什么使用SQL;sum";返回一个假值,android,sql,sqlite,Android,Sql,Sqlite,我有3张表格,我想返回 客户姓名和总额每个存款和信贷金额的总和 deposit customers credit id id id d_amount c_amount customer_id name customer_id type(credit,etc) 我是通过这个查询来完成的=> SELECT c

我有3张表格,我想返回 客户姓名和总额每个存款信贷金额的总和

deposit        customers     credit
id             id            id
d_amount                     c_amount
customer_id    name          customer_id
                             type(credit,etc)
我是通过这个查询来完成的=>

SELECT  customers.name ,
sum(deposit.d_amount) as total_depot,
sum(credit.c_amount) as total_credit
from customers
inner join  deposit on deposit.customer_id  = customers.id
inner join  credit on credit.customer_id  = customers.id
and credit.type='credit'
group by customers.id order by customers.name asc
不幸的是,总仓库总信用的结果不正确 但当我像这样单独做时=>

SELECT  customers.name , sum(deposit.d_amount) as total_depot
from customers
inner join  deposit on deposit.customer_id  = customers.id
group by customers.id order by customers.name asc


SELECT  customers.name , sum(credit.d_amount) as total_credit
from customers
inner join  credit on credit.customer_id  = customers.id
and credit.type='credit'
group by customers.id order by customers.name asc
总仓库总信用的结果是正确的 我不知道错误在哪里。

在加入表之前进行聚合:

select c.name, d.total_deposit, cr.total_credit
from customers c join
     (select d.customer_id, sum(d.d_amount) as total_deposit
      from deposit d
      group by d.customer_id
     ) d
     on d.customer_id = c.id join
     (select c.customer_id, sum(c.c_amount) as total_credit
      from credit c
      where c.type = 'credit'
      group by c.customer_id
     ) cr
     on cr.customer_id = c.id
order by c.name asc;
在加入表之前执行聚合:

select c.name, d.total_deposit, cr.total_credit
from customers c join
     (select d.customer_id, sum(d.d_amount) as total_deposit
      from deposit d
      group by d.customer_id
     ) d
     on d.customer_id = c.id join
     (select c.customer_id, sum(c.c_amount) as total_credit
      from credit c
      where c.type = 'credit'
      group by c.customer_id
     ) cr
     on cr.customer_id = c.id
order by c.name asc;

第一个查询完全错误,连接将在结果中增加行数。例如,一名客户、3名他的信用卡和5名他的存款:

从客户返回1行中选择

客户内部加入积分返回3行

客户内部加入积分内部加入存款返回15行

那不是你想要的。不使用SUM和groupby执行查询,您将看到它

这就是您想要的(简化,未经测试):


顺便说一句,如果客户既没有存款也没有信用卡,则需要使用左联接

第一次查询完全错误,联接将在结果中增加行数。例如,一名客户、3名他的信用卡和5名他的存款:

从客户返回1行中选择

客户内部加入积分返回3行

客户内部加入积分内部加入存款返回15行

那不是你想要的。不使用SUM和groupby执行查询,您将看到它

这就是您想要的(简化,未经测试):


顺便说一句,如果客户既没有存款也没有信用卡,则需要左键联接

谢谢大家'

我尝试了所有的考试,但它不起作用

我终于做到了它的工作。

select customers.name,  total_depot , total_credit
from customers 
left join (select customer_id , d_amount , sum(deposit.d_amount) as total_deposit from deposit group by customer_id)  d on d.customers_id = customers.id 
left join (select customer_id , c_amount , sum(credit.c_amount) as total_fact from credit where credit.type='credit' group by customer_id)  c on c.id = customers.id 
group by customers.name

非常感谢大家'

我尝试了所有的考试,但它不起作用

我终于做到了它的工作。

select customers.name,  total_depot , total_credit
from customers 
left join (select customer_id , d_amount , sum(deposit.d_amount) as total_deposit from deposit group by customer_id)  d on d.customers_id = customers.id 
left join (select customer_id , c_amount , sum(credit.c_amount) as total_fact from credit where credit.type='credit' group by customer_id)  c on c.id = customers.id 
group by customers.name

我看不出你的第一个查询不起作用的简单原因。我可以看到您使用的是
group by
customers.id而不是name。我想您执行此查询时应该会出错。我看不出第一个查询不起作用的简单原因。我可以看到您使用的是
groupby
customers.id而不是name。我想执行此操作时应该会出错query@Birante . . . 嗯,那个评论毫无用处,对解决你的问题毫无帮助。@Birante。好吧,那评论完全没有用,对解决你的问题没有任何帮助。