2013-04-08 103 views
-2

我正在計算名爲ptb_profile_views的mysql表中的行數。如果列數據重複,mysql不會重複計數兩次

我的表看起來像這樣:

id | profile_id | viewed_profile_id | date_time | 

1   1     6    
2   2     6 
3   2     6 
4   2     6 
5   3     6 

此刻我的查詢計算表中的所有行。但是,我只希望它對不包含重複值的行在profile_id中進行計數。

因此,如果用戶2/profile_id 2多次查看用戶6的配置文件,在這種情況下他們這樣做,我只希望查詢計算一次。我試圖使用不同的,但它不工作。有人可以告訴我我要去哪裏嗎?

function check_profile_views() { 
    global $connection; 
    global $_SESSION; 
    $query = "SELECT DISTINCT COUNT(id) FROM ptb_profile_views WHERE viewed_profile_id=".$_SESSION['user_id']." AND profile_id='0'"; 
    $check_profile_views_set = mysql_query($query, $connection); 
    confirm_query($check_profile_views_set); 
    return $check_profile_views_set;   
} 
+0

重複http://stackoverflow.com/questions/15870709/mysql-do-not-count-duplicate-rows – Amir 2013-04-08 03:26:23

回答

0

嘗試下面的(假定viewed_profile_id是6):

SELECT COUNT(DISTINCT profile_id) FROM ptb_profile_views WHERE viewed_profile_id=6