2011-08-22 62 views
0

我有以下查詢如何顯示有多少人需要mySQL中的產品?

$query2 = "SELECT reverse_products.id, reverse_products.name, reverse_relations.user_id 
FROM reverse_products JOIN reverse_relations ON reverse_products.id = reverse_relations.product_id 
WHERE product_id = $dealid "; 

$row = mysql_fetch_array(mysql_query($query2)); 

echo "$row[id]: $row[name]"; 

,顯示了產品的IDreverse_products表。在reverse_relations表中存儲* user_id *和* product_id *。有許多* user_id *可能具有相同的* product_id *。

如何更改我的查詢以顯示: 有多少* user_id *具有相同的* product_id *?我需要第二個查詢嗎?

回答

0
SELECT count(user_id) 
FROM reverse_relations 
GROUP BY product_id 
WHERE product_id = $dealid 

如果我正確理解你的表格結構。

+0

我真的需要第二個查詢呢? (用sql方式進行) – EnexoOnoma

+0

如果你想從查詢計數,是的。你必須使用group-by子句,因爲它使用了一個聚合函數,並且也獲取了非聚合結果,並且這會破壞單個行 –

0
count(reverse_relations.user_id) as num 
echo $row[num] 
相關問題