2011-05-22 122 views
2

以下查詢顯示多個同一用戶,如何使其唯一?唯一記錄查詢

$query = 'SELECT a.connection_id, a.connect_from, a.connect_to, 
        b.userid, b.thumb, c.name, d.user_id, d.field_id, 
        d.value as bday, e.creator, e.id as videoprofile, 
        DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),d.value)), "%Y")+0 AS age 
      FROM  `#__community_connection` AS a 
        LEFT JOIN `#__community_users` AS b 
         ON a.connect_to = b.userid 
        LEFT JOIN `#__users` AS c ON a.connect_to = c.id 
        LEFT JOIN `#__community_fields_values` AS d 
         ON a.connect_to = d.user_id 
        LEFT JOIN `#__community_videos` AS e ON a.connect_to = e.creator 
      WHERE a.connect_from = "' . $uid .'" AND d.field_id = "3" 
      ORDER BY DAYOFMONTH(bday) ASC'; 

回答

2

在您的SELECT後面添加DISTINCT僅返回唯一行。所以,你的代碼的第一行更改爲以下:

$query = 'SELECT DISTINCT a.connection_id, a.connect_from, a.connect_to, ... 
[The rest of your query follows here.] 
+1

+1,但是票數不足 – Johan 2011-05-22 22:21:47

+0

@Johan,+1對您對道德支持的評論。 :-) – 2011-05-25 14:23:01

0

不完全確定你在問什麼,但我想你想爲你的用戶提供一種獨特的識別方法?

那麼在PHP中,你可以哈希他們的IP地址,並使用它作爲自己的ID,但是它包含數字和字母,這樣會使其唯一我想:)

下面是它如何會看

.md5($data['post_ip']); 

它會顯示給每個不同的,例如,我的顯示如下 e36e263f082188a317f89e0dfef766ed

希望這是你在找什麼:)

+0

-1,OP要消除重複的行他現在得到他的查詢,添加一個'distinct'將這樣做。一個自動增量編號會做獨特的罰款,沒有MD5魔術需要恕我直言。還要注意這個問題被標記爲'query'而不是'php' :-)。 – Johan 2011-05-22 22:23:24