0

在我的应用程序中,我想显示我所有的朋友以及发出和收到的支票

Table Transaction                 Table Friends
----------------------------      -----------------------------
id  given_id   rev_id   amt        id  who_id  whom_id  who_name
-----------------------------     -------------------------------
 1    2          1       1k         1     1       2       sss
 2    2          3       1k         2     3       2       fff
 3.   3          2       2k         3     4       1       eee
 4    1          2       2k         4     2       1       iii
-----------------------------      ------------------------------

Result whom_id=2 name=iii  -> Friends (sss,fff) 
                            => sss gives totally 1 cheque and 
                               sss receives totally 1 cheque and
                               fff gives 1 cheque and
                               fff receives 1 cheque and

这个我试过了。。

SELECT 
p.who_id, 
p.who_name, 
COUNT( r1.give_id ) , 
COUNT( r1.rec_id ) 

FROM 
friends p 
LEFT JOIN Transaction r1 

ON p.who_id = r1.give_id OR p.who_id = r1.rec_id 

WHERE 
p.whom_id = 1 

GROUP BY p.who_id

请给我最好的方法来做到这一点......

4

5 回答 5

0

您可以将 HAVING 与 group by 一起使用,而不是

WHERE p.whom_id = 1 
于 2013-01-30T10:58:42.313 回答
0

你可以这样做

SELECT
  f.who_name,
  count(t.given_id)    GivenTotal,
  count(lt.rev_id) as RecievedTotal
from friends as f
  left join transaction as t
    on t.given_id = f.who_id
  left join transaction as lt
    on lt.rev_id   = f.who_id    
where f.whom_id = 2
group by t.given_id

演示

输出

who_name | GivenTotal | RecievedTotal 
----------------------------------
sss      |  1         |     1 
fff      |  1         |     1 
于 2013-01-30T11:09:22.387 回答
0

有两种方法可以做到这一点,都涉及在Transactions桌子上加入两次。

您可以使用子查询:

SELECT f.who_id,
  f.who_name,
  t1.TotalGiven,
  t2.TotalRev
FROM friends f
LEFT JOIN
(
  select count(t.given_id) TotalGiven, t.given_id
  from Transactions t
  group by t.given_id
) t1
  ON f.who_id = t1.given_id 
LEFT JOIN
(
  select count(t.rev_id) TotalRev, t.rev_id
  from Transactions t
  group by t.rev_id
) t2
  ON f.who_id = t2.rev_id 
where f.whom_id = 2;

请参阅SQL Fiddle with Demo

或者您可以只使用没有子查询的连接:

select f.who_id,
  f.who_name,
  count(t1.given_id) TotalGiven,
  count(t2.rev_id) TotalRev
FROM friends f
LEFT JOIN Transactions t1
  ON f.who_id = t1.given_id 
LEFT JOIN Transactions t2
  ON f.who_id = t2.rev_id 
where f.whom_id = 2
group by f.who_id,  f.who_name

请参阅带有演示的 SQL Fiddle

于 2013-01-30T11:01:59.267 回答
0

最后我得到了答案,

SELECT 
p.who_id, p.who_name, COUNT( r1.give_id ) , COUNT( r2.rec_id ) 

FROM 
friends p 
LEFT JOIN Transaction r1  ON p.who_id = r1.give_id 
LEFT JOIN Transaction r2  on p.who_id = r2.rec_id
WHERE 
p.whom_id = 1 

GROUP BY p.who_id
于 2013-01-31T11:41:32.510 回答
0

尝试这个,

select
  id,
  count(given_id) as given,
  count(rev_id) as reveive
from Friends
GROUP BY id;

如果你想要两个金额之间的差异,试试这个

select
  id,
  count(given_id) as given,
  count(rev_id) as reveive,
  given-receive as diff
from Friends
GROUP BY id;
于 2013-01-30T10:48:45.093 回答