0

2 行のデータをカウントとして返すクエリがあります。列の値の合計を求めたい。私のSQL:

(SELECT count(memberid) count 
   FROM `friendRequest`, Member 
   where status = 2 and memberid = sender and receiver = 19) 
UNION 
(SELECT count(memberid) count 
   FROM `friendRequest`, Member 
   where status = 2 and memberid = receiver and sender = 19)

使用してみSelect Sum(my above query)ましたが、エラーが発生します。上記の値のいくつかが必要です。

4

4 に答える 4

4

1 つのクエリで両方の結果セットを取得できます。

SELECT count(memberid) count 
FROM `friendRequest`, Member 
WHERE status = 2 AND (
    (memberid = sender AND receiver = 19) OR 
    (memderid = receiver AND sender = 19)
)
于 2012-06-14T19:28:05.390 に答える
0

試す:

SELECT SUM(count)
FROM
  ...the rest of your query...
于 2012-06-14T19:20:47.540 に答える
0

集計を使用する場合は、何かを使用してグループ化する必要がありますGROUP BY

(SELECT count(memberid) SUM(count) 
   FROM `friendRequest`, Member 
   where status = 2 and memberid = sender and receiver = 19
   GROUP BY Member) 
UNION 
(SELECT count(memberid) SUM(count) 
   FROM `friendRequest`, Member 
   where status = 2 and memberid = receiver and sender = 19
   GROUP BY Member)
于 2012-06-14T19:30:15.510 に答える
0

以下はそのまま動作するはずです

SELECT SUM(count)
FROM   (
         (SELECT count(memberid) count 
          FROM `friendRequest`, Member 
          where status = 2 and memberid = sender and receiver = 19) 
          UNION 
         (SELECT count(memberid) count 
          FROM `friendRequest`, Member 
          where status = 2 and memberid = receiver and sender = 19)
       ) AS anyAliasWillDo

ただし、暗黙的な結合(廃止)の使用をやめ、明示的な結合構文の使用を開始することを強くお勧めします。ステートメントは次のようになります

SELECT SUM(COUNT)
FROM   (
            SELECT  COUNT(memberid) AS COUNT 
            FROM    `friendRequest` AS fr
                    INNER JOIN Member AS m ON m.memberid = fr.sender
            WHERE   status = 2 AND receiver = 19 
            UNION ALL
            SELECT  COUNT(memberid) AS COUNT 
            FROM   `friendRequest` AS fr 
                    INNER JOIN Member AS m ON m.memberid = fr.receiver
            WHERE   status = 2 AND sender = 19
        ) AS AnyAliasWillDo
于 2012-06-14T19:30:23.463 に答える