2011-04-21 65 views
0

我的信息在一個表像這樣:使用MySQL的COUNT數多列

id name recommender 
1 daniel steve 
2 daniel tony 
3 steve daniel 

和驗證碼:

<h2>Follow/Join Recommendation League Table</h2> 
<br/> 
<?php 

$query = "SELECT name_of_follower, COUNT(name) FROM recommendation_competition_entrants GROUP BY name_of_follower ORDER BY COUNT(name) DESC"; 

$result = mysql_query($query) or die(mysql_error()); 

// Print out result 
while($row = mysql_fetch_array($result)){ 
     echo $row['name_of_follower'] . " has ". $row['COUNT(name)'] . " entries."; 
     echo "<br />"; 
} 

?> 

這個計算所有條目的「名稱」欄,並顯示它看起來像這樣小聯賽積分榜:

daniel - 2 entries 
steve - 1 entry 

我現在需要做的是從兩個名字數名推薦列,所以表格看起來像這樣:

daniel - 3 entries 
steve - 2 entries 
tony - 1 entry 

有沒有簡單的方法來做到這一點?

感謝所有幫助

回答

1
select name_of_follower, 
     count(name_of_follower) 
    from (select name as name_of_follower 
      from abc 
     union all 
     select follower as name_of_follower 
      from abc 
     ) t 
group by t.name_of_follower 
order by count(name_of_follower) desc 
+0

謝謝:)別的東西已經到來了,但我得到一個機會,我會盡快嘗試了這一點 – 2011-04-21 13:01:23

0

SELECT SUM(COUNT(name) + COUNT(recommender)) FROM...