2011-06-26 12 views

答えて

1

あなたが外でカウントし、すべてのテーブル組合の結果を含むサブクエリにそれを適用配置しようとしました:あなたはのようなものは、質問に答えるために

SELECT COUNT(*) FROM (SELECT ...) as abc 

それとも

Select mytable .userid, sum(mytable .subcount) as totalcount from 
(
select userid, count(*) as subcount from table1 group by userid 
union all 
select userid, count(*) as subcount from table2 group by userid 
) 
as mytable 
group by mytable .userid 

またはFULL OUTER JOINの代わりに労働組合を使用してみてください、これを試して、それはあなたの...

SELECT Count(UserID), UserId 
FROM MyTable1 
GROUP BY MyTable1.UserID 
UNION 
SELECT Count(UserID), UserId 
FROM MyTable2 
FULL OUTER JOIN MyTable2 ON (MyTable1.UserId=MyTable2.UserId) 
GROUP BY MyTable2.UserID 

更新の答えも同じ結果が得られます:

IFあなたのクエリはうまくいきますeはその後、クエリがOK

select count(*) from 
    (
    SELECT COUNT(*) as num 
    from table_one LEFT JOIN table_constant on table_one.c_id 
    = table_constant.c_id 
    where table_constant.user_id = '$uid' 
UNION 
SELECT COUNT(*) as num 
from table_two LEFT JOIN table_constant on table_two.c_id 
= table_constant.c_id 
where table_two.added_by = '$uid' 
UNION 
SELECT COUNT(*) as num 
from table_three LEFT JOIN table_constant ON table_three.c_id 
= table_constant.c_id 
where table_constant.user_id = '$uid' 
UNION 
SELECT COUNT(*) as num 
from table_four LEFT JOIN table_constant ON table_four.c_id 
= table_constant.c_id 
where table_constant.user_id = '$uid' 
ORDER BY date_time_added DESC") as pagecount 
+0

ねえ。私は上記のように$ total_pagesであるvariabelの中のカウントが必要です。私はそれを行う方法がわかりません。 – KPO

+0

私は自分の答えを更新しました。これをチェックしてください。照会で組合ルールを実行している場合は、これを行います。 mycountとして(あなたのクエリ)からcount(*)を選択します。それは私のために働いた今あなたはあなたの問題を取り除くでしょう.. – Syeda

+0

もう一つの事Syeda。どのように私はそれを変数に入れますか? – KPO

2

さらに別のクエリですべてを包んで合計します。

SELECT sum(num) 
FROM (... union queries here ...) as subquery; 

PHPで返された行をループし、合計を自分で行います。

1

これを書くには、より良い方法が必要です。ユニオンは非常に強力ですが、1つのクエリで4つの選択肢を呼び出すのですが、それがすべてのページで実行されると、パフォーマンスが低下します。

SELECT 
    SUM (mnTbl.num) as sumNum 
FROM 
    (
     SELECT 
      COUNT(*) as num 
     FROM 
       table_one 
      LEFT JOIN 
       table_constant 
      ON 
       table_one.c_id = table_constant.c_id 
     WHERE 
      table_constant.user_id = '$uid' 
    UNION 
     SELECT 
      COUNT(*) as num 
     FROM 
       table_two 
      LEFT JOIN 
       table_constant 
      ON 
       table_two.c_id = table_constant.c_id 
     WHERE 
      table_two.added_by = '$uid' 
    UNION 
     SELECT 
      COUNT(*) as num 
     FROM 
       table_three 
      LEFT JOIN 
       table_constant 
      ON 
       table_three.c_id = table_constant.c_id 
     WHERE 
      table_constant.user_id = '$uid' 
    UNION 
     SELECT 
      COUNT(*) as num 
     FROM 
       table_four 
      LEFT JOIN 
       table_constant 
      ON 
       table_four.c_id = table_constant.c_id 
     WHERE 
      table_constant.user_id = '$uid' 
    ) as mnTbl 
ORDER BY 
    date_time_added DESC 
+0

は今あなたをしよう.....このようなものだ

select count(*) from (your query) as pagecount.. 

を意味します。 – KPO

+0

期待どおり、実際にはいくつかのサンプルデータを使ってSQL文をテストすることはできません – Ben

+0

何をmnTbl.numに置きますか?テーブル名? – KPO

関連する問題