如何使用连接从两个表中收集数据

时间:2014-10-10 18:00:54

标签: mysql sql

我有2张桌子 表1结构:[日志]
userid ||点击||日期|| XXX || XXX |||
表2结构:[用户]
用户名||电子邮件||日期|| XXX || XXX |||

我想显示这样的数据

用户名,COUNT(点击次数)

我正在使用下面的查询。

SELECT `user.username`,`userid`, COUNT(`userid`) as `total` 
FROM `logs` 
    INNER JOIN user ON logs.userid=user.userid 
ORDER BY total DESC 
LIMIT 5

请定义

在第一次回复之后,我修改了查询并提供了它,就像这个

 $sqlptcwall=mysql_query("SELECT user.username, COUNT(ptcwalllogs.userid) AS total FROM logs INNER JOIN user ON user.userid = ptcwalllogs.userid GROUP BY user.username ORDER BY COUNT(ptcwalllogs.userid) DESC
LIMIT 5");



<?php
echo "</tr>";
      while($row = mysql_fetch_array($sqlptcwall)) 
      {
echo "<tr> ";
     echo "<td>" .$row[userid] . "</td>";
     echo "<td>" .$row[total] . "</td>";
     }
echo "</tr> " ;
?>

错误: mysql_fetch_array()期望参数1是资源,在

中给出布尔值

1 个答案:

答案 0 :(得分:1)

SELECT user.username, COUNT(user.userid) AS total
FROM logs
    INNER JOIN user ON user.userid = logs.userid
GROUP BY user.username
ORDER BY COUNT(user.userid) DESC
LIMIT 5