Php 联接查询不工作
我使用三个MySQl表: 评论Php 联接查询不工作,php,mysql,Php,Mysql,我使用三个MySQl表: 评论 commentid loginid submissionid comment datecommented 登录 loginid username password email actcode disabled activated created points 服从 submissionid loginid title url displayurl datesubmitted 在这三个表中,“loginid”对应 我想根据“submissionid”的数量来列
commentid loginid submissionid comment datecommented
登录
loginid username password email actcode disabled activated created points
服从
submissionid loginid title url displayurl datesubmitted
在这三个表中,“loginid”对应
我想根据“submissionid”的数量来列出前10个LoginID。我想在一个3列的HTML表格中显示它们,第一列显示“用户名”,第二列显示“submissionid”的数量,第三列显示“commentid”的数量
我尝试使用下面的查询,但没有成功。知道为什么吗
提前感谢,
约翰
$sqlStr=“选择
l、 用户名
,l.loginid
,c.commentid
,count(s.commentid)countComments
,c.评论
,c
,s.submissionid
,count(s.submissionid)countSubmissions
,s.title
,s.url
,s.displayurl
,s.date提交
从注释中选择c
在c.loginid=l.loginid上以l的身份登录内部联接
在c.loginid=s.loginid上作为s提交内部联接
c.loginid集团
按计数排序
限制10”;
$result=mysql\u查询($sqlStr);
$arr=array();
回声“;
而($row=mysql\u fetch\u数组($result)){
回声';
回声';
回声';
回显“”。条带斜杠($row[“countSubmissions”]);
回声';
回声';
回显“”。条带斜杠($row[“countComments”]);
回声';
}
回声“;
在查询中,您选择了非组项目,如commentid、comment等。
这应该会产生预期的结果
选择l.username,将(s.submissionid)计数为NoOfSubmissions,将(c.commentid)计数为NoOfComments,从c.submissionid=s.submissionid上的注释c内部加入提交中选择
l.loginid=c.loginid组上的内部连接登录名l按l.username按计数排序(s.submissionid)限制10
谢谢
林森克
数据库管理员
91 + 9995044142
www.qburst.com谢谢。。。这似乎只显示至少有一个submissionid的LoginID。如何让它显示没有SubmissionID的LoginID?右,然后用左连接替换连接。请查看更新的查询,它还包括将NULL值转换为0。
$sqlStr = "SELECT
l.username
,l.loginid
,c.commentid
,count(s.commentid) countComments
,c.comment
,c.datecommented
,s.submissionid
,count(s.submissionid) countSubmissions
,s.title
,s.url
,s.displayurl
,s.datesubmitted
FROM comment AS c
INNER JOIN login AS l ON c.loginid = l.loginid
INNER JOIN submission AS s ON c.loginid = s.loginid
GROUP BY c.loginid
ORDER BY countSubmissions DESC
LIMIT 10";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec1\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1"><a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.stripslashes($row["username"]).'</a></td>';
echo '</tr>';
echo '<td class="sitename1">'.stripslashes($row["countSubmissions"]).'</td>';
echo '</tr>';
echo '</tr>';
echo '<td class="sitename1">'.stripslashes($row["countComments"]).'</td>';
echo '</tr>';
}
echo "</table>";
select
l.username,
s.submissions,
c.comments
from
l,
(
select
count(s.submissionid) as submissions,
s.loginid
from
submission s
group by
s.loginid
) s,
(
select
count(c.commentid) as commentids,
c.loginid
from
comment c
group by
c.loginid
) c
where
l.loginid = s.loginid and
l.loginid = c.loginid
order by
s.submissions desc
limit
10
SELECT
l.loginid,
l.username,
COALESCE(s.total, 0) AS numSubmissions,
COALESCE(c.total, 0) AS numComments
FROM login l
LEFT JOIN (
SELECT loginid, COUNT(1) AS total
FROM submission
GROUP BY loginid
) s ON l.loginid = s.loginid
LEFT JOIN (
SELECT loginid, COUNT(1) AS total
FROM comment
GROUP BY loginid
) c ON l.loginid = c.loginid
GROUP BY l.loginid
ORDER BY numComments DESC