简体   繁体   English

一个查询中有两个计数

[英]two counts in one query

I have this table: 我有这张桌子:

id  | id_user  |  id_user_stalkers | date
0   |  0222222 |  032332         | 32234234

so, i want to know count of all users that are in my friends list, and cout of all users that have me in their friend list. 因此,我想知道我的朋友列表中所有用户的数目,以及知道我的朋友列表中所有用户的数目。 at the moment, i've made this query : 目前,我已经执行了以下查询:

SELECT (
                    SELECT COUNT(id_user)
                    FROM   stalkers WHERE id_user =  ".$id."
                    ) AS user_stalkers,
                    (
                    SELECT COUNT(id_user_stalkers)
                    FROM   stalkers WHERE id_user_stalkers = ".$id."
                    ) AS user_is_stalked
            FROM stalkers  

but it returns this: 但它返回以下内容:

Array
(
    [0] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [1] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [2] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        )

    [3] => Array
        (
            [user_stalkers] => 7
            [user_is_stalked] => 2
        ))

it's all right, but i need only one row, not four. 没关系,但我只需要一行,而不需要四行。

Can somebody help me please ? 有人可以帮我吗?

Since all your fields are subqueries you can drop the from clause altogether. 由于您的所有字段都是子查询,因此您可以完全删除from子句。 Otherwise you get a subquery for every single row in the table. 否则,您将为表中的每一行获得一个子查询。 This not only gets the results you want but reduces total hit on the database by a potentially very large amount (increasing performance). 这不仅可以获取所需的结果,而且还可以减少数据库上的总命中次数(可能会非常大)(提高性能)。

SELECT 
(
 SELECT COUNT(id_user)
 FROM   stalkers WHERE id_user =  ".$id."
) AS user_stalkers,
(
 SELECT COUNT(id_user_stalkers)
 FROM   stalkers WHERE id_user_stalkers = ".$id."
) AS user_is_stalked

If this is going to be a potentially large table you'll want to make sure id_user and id_user_stalkers are both indexed. 如果这将是一个可能很大的表,则需要确保id_user和id_user_stalkers都已索引。 If an index can be used these sub-queries will be much much faster. 如果可以使用索引,那么这些子查询会快得多。

You have to add one more where clause as below and distinct: 您必须再添加一个where子句,如下所示且与众不同:

SELECT DISTINCT (
                    SELECT COUNT(id_user)
                    FROM   stalkers WHERE id_user =  ".$id."
                    ) AS user_stalkers,
                    (
                    SELECT COUNT(id_user_stalkers)
                    FROM   stalkers WHERE id_user_stalkers = ".$id."
                    ) AS user_is_stalked
FROM stalkers  
WHERE id_user_stalkers = ".$id."

A safe way to set up this query for any database is to use a cross join : 为任何数据库设置此查询的安全方法是使用cross join

SELECT user_stalkers, user_is_stalked
from (SELECT COUNT(id_user) as user_stalkers
      FROM   stalkers WHERE id_user =  ".$id."
     ) t1 cross join
     (SELECT COUNT(id_user_stalkers) as user_is_stalked
      FROM   stalkers WHERE id_user_stalkers = ".$id."
     )  t2

Your query is returning one row for every row in Stalkers because of the FROM clause. 由于FROM子句,您的查询在Stalkers中的每一行都返回一行。

         SELECT (
                SELECT COUNT(id_user)
                FROM   stalkers WHERE id_user =  ".$id."
                ) AS user_stalkers,
                (
                SELECT COUNT(id_user_stalkers)
                FROM   stalkers WHERE id_user_stalkers = ".$id."
                ) AS user_is_stalked

Just remove the last "FROM stalkers" 只需删除最后的“ FROM缠扰者”

Try this (you dont have to query twice and worry about distinct etc) 尝试此操作(您不必查询两次,也不必担心出现不同的情况等)

SELECT COUNT(case when id_user =  ".$id." then 1 else null) AS user_stalkers,
COUNT(case when id_user_stalkers =  ".$id." then 1 else null) AS user_is_stalked
FROM   stalkers

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

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