简体   繁体   English

在一个查询SQL中获得好友总数

[英]Get totals of friends in one query SQL

I want to get the total of friends on one QUERY SQL 我想在一个QUERY SQL上获得全部朋友

I have a table named MEMBERS , and this table have a CALL_FRIEND_ID , 我有一个名为MEMBERS的表,该表具有CALL_FRIEND_ID

the CALL_FRIEND_ID is a random ID you give it to your friend and if he/she register using, he/she become your friend by adding your to a column named FRIEND_ID (in the same table) CALL_FRIEND_ID是您提供给朋友的随机ID,如果他/她注册使用,他/她通过将您添加到名为FRIEND_ID的列(在同一表中)而成为您的朋友

Now to know the total friend of each one I make two query 现在知道每个朋友的总和,我提出两个查询

SELECT * FROM MEMBERS;

second 第二

SELECT COUNT(*) FROM MEMBERS WHERE FRIEND_ID = :id

but that make the response slowly 但这会使响应缓慢

I try to use HAVING but without any result, any solution ? 我尝试使用HAVING但是没有任何结果,没有任何解决方案?

Try this query: from your scenario I guess this'll give your desired output 尝试以下查询:从您的方案中,我想这将提供您想要的输出

 select FRIEND_ID,count(CALL_FRIEND_ID) as totalFriends from MEMBERS 
    where FRIEND_ID = :id
    group by FRIEND_ID

As you said when a member register then a random number is assigned to his or her that CALL_FRIEND_ID ,and you want total number of friends in your member table according to group wise of FRIEND_ID so there is need where clause,because where will be applicable if you find any specific FRIEND_ID, so you can use below query 就像您说的那样,当会员注册时,会向他或她分配一个随机数CALL_FRIEND_ID,并且您希望根据FRIEND_ID的分组方式在您的会员表中添加好友总数,因此需要where子句,因为如果您可以找到任何特定的FRIEND_ID,因此可以在下面的查询中使用

select FRIEND_ID,count(CALL_FRIEND_ID) as totalFriends from MEMBERS 
        group by FRIEND_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM