2012-01-29 52 views
0

我的查詢:計數數據

  "SELECT id 
      FROM user_follow 
      WHERE user_id IN(3,6) AND follow_id NOT IN(3,6) 
      GROUP BY follow_id"; 

,它顯示:

[users] => Array 
    (
     [0] => Array 
      (
       [id] => 20 
      ) 

     [1] => Array 
      (
       [id] => 9 
      ) 

     [2] => Array 
      (
       [id] => 19 
      ) 

    ) 

我需要統計數據(在這個例子中查詢必須歸還3的結果)


我試過COUNT(ID)但結果對我不好。

結果:

 [0] => Array 
      (
       [count(id)] => 1 
      ) 

我試圖SELECT COUNT(1)

結果:

 [0] => Array 
      (
       [count(1)] => 1 
      ) 

     [1] => Array 
      (
       [count(1)] => 2 
      ) 

     [2] => Array 
      (
       [count(1)] => 1 
      ) 

還沒不是一數(3)。我想是這樣的[用戶] => 3

+1

你是什麼意思的「效果不好」?你有錯誤嗎?不是正確的號碼嗎? – jprofitt 2012-01-29 00:14:50

+0

刪除'GROUP BY'然後....並只是'SELECT COUNT(DISTINCT id)' – Wrikken 2012-01-29 00:15:56

+0

我不能刪除GROUP BY follow_id,因爲http://stackoverflow.com/questions/9049401/select-all-my -and-my-friends-friends-except-us-both-sql此組防止顯示重複的朋友 – Viktors 2012-01-29 00:35:14

回答

2

如果您只需要數量,而不是數據本身,你可以試試這個:

SELECT count(1) 
FROM user_follow 
WHERE user_id IN(3,6) AND follow_id NOT IN(3,6) 
GROUP BY follow_id 
+0

我只想找回一個數字(例如3) – Viktors 2012-01-29 00:27:31

+0

我試過了你的查詢,不顯示一個數字。結果是像東西count(id) – Viktors 2012-01-29 00:28:19

+0

然後'SELECT count(1)'將工作 – Mike 2012-01-29 00:28:49

1
$count=mysql_num_rows($sql); 

你可以試試這個

1

如果我理解你的需要,你可以使用陣列本身上數如下:

   $count=count($users)