这是我的代码
$getlast = mysql_query("SELECT * FROM `offersdone` group by uid");
$content .= "<table><tr><th>Username</th><th>Total Offers</th></tr>";
while($last = mysql_fetch_array($getlast)){
$getuser = mysql_query("SELECT * FROM `users` WHERE ID='".$last['uid']."'");
if (($user = mysql_fetch_array($getuser)){
$getlast1 = mysql_query("SELECT * FROM `offersdone` WHERE uid='".$user['ID']."' ");
$num1 = mysql_num_rows($getlast1);
$content .= "<tr><td>" . $user['username'] . " </td><td> " . $num1 . "</td></tr>";
}
}
结果如下:
Username Total Offers
martin 15
dragan 1
mile1986 1
naramix 7
rrrob 55
sha09hh 17
shakest 1
答案 0 :(得分:0)
$sql = "SELECT
u.username,
count(*) as count
FROM
offersdone o
LEFT JOIN
users u
ON u.ID = o.uid
GROUP BY
o.uid
ORDER BY
count(*) DESC";
$getlast = mysql_query($sql);
$content .= "<table><tr><th>Username</th><th>Total Offers</th></tr>";
while($last = mysql_fetch_array($getlast)){
$content .= "<tr><td>" . $last['username'] . " </td><td> " . $last['count'] . "</td></tr>";
}
这应该选择用户名和一个查询的商品数量,并且已经按商品数量排序。
演示:
mysql> SELECT * FROM offersdone;
+-----+
| uid |
+-----+
| 1 |
| 1 |
| 2 |
| 2 |
| 2 |
| 2 |
+-----+
6 rows in set (0.00 sec)
mysql> SELECT * FROM users;
+----+----------+
| ID | username |
+----+----------+
| 1 | A |
| 2 | B |
+----+----------+
2 rows in set (0.00 sec)
mysql> SELECT u.username, COUNT( * ) as count
-> FROM offersdone o
-> LEFT JOIN users u ON u.ID = o.uid
-> GROUP BY o.uid
-> ORDER BY COUNT( * ) DESC;
+----------+-------+
| username | count |
+----------+-------+
| B | 4 |
| A | 2 |
+----------+-------+
2 rows in set (0.00 sec)
答案 1 :(得分:0)
添加到您的查询&#34; ORDER BY Total提供ASC&#34;;