开发者

Mysql count returns empty while using groupby

This question asked many times in stack overflow before.I tried all solutions,But none of them helps.

my query is :-

 SELECT COUNT(*) FROM  
  table_campaign_referralmail t1 
  INNER JOIN table_user_information t2 ON t1.refferal_id=t2.user_id 
  GROUP BY t1.refferal_id

return empty results

t1 is empty,t2 has some records

Tried query and result

SELECT CASE  WHEN COUNT(*) IS NULL THEN '0' ELSE COUNT(*) END 
FROM   table_campaign_referralmail t1 INNER JOIN table_user_information t2 ON 
t1.refferal_id=t2.user_id GROUP BY t1.refferal_id

return empty results

COALESCE(count(*),0) instant of count(*)

return empty results

COUNT(t1.*) instant of count(*)

return error result

Edit

t2 has the basic info of user. t1 is the referral table.so, it has number of user referred friends. so it may comes multiple times. like

refferal_id  friend_id 
   1            2
   1            3

so, i want to dis开发者_运维知识库play number of referrals per user

like

user_id   number of referal
1               2

This is what i am doing .

please advise me thanks


"t1 is empty,t2 has some records" of course it will return empty, because you are doing an INNER JOIN. How will you count the t1 records grouped by refferal_id if there ain't any in it?

Tell us what you want to do, and we might come up with a re-arranged query to do just the thing you want to do.


this will get you number of referrals per user:

SELECT COUNT(t1.refferal_id)
  FROM table_user_information t2
  LEFT JOIN table_campaign_referralmail t1 ON t1.refferal_id=t2.user_id 
 GROUP BY t2.user_id 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜