创建一个等于连接查询结果的新变量

时间:2011-05-18 00:06:06

标签: php mysql join

我在一个名为$u的会话变量的页面上运行下面的查询。我想从$points等于totalScore2的查询中创建一个名为l.username的变量,其等于$u。字段l.username仅等于$u一次。

我该怎么做?

提前致谢,

约翰

$sqlStr5 = "SELECT 
    l.loginid, 
    l.username, 
    l.created,
    DATEDIFF(NOW(), l.created) + COALESCE(s.total, 0) * 5 + COALESCE(scs.total, 0) * 10 - COALESCE(nscs.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore2
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

LEFT JOIN (
    SELECT S2.loginid, COUNT(1) AS total 
    FROM submission S2
    INNER JOIN comment C2
    ON C2.submissionid = S2.submissionid
    GROUP BY S2.loginid
) scs ON scs.loginid = l.loginid


LEFT JOIN (
    SELECT S2.loginid, COUNT(1) AS total 
    FROM submission S2
    INNER JOIN comment C2
    ON C2.submissionid = S2.submissionid
    AND C2.loginid = S2.loginid
    GROUP BY S2.loginid
) nscs ON nscs.loginid = l.loginid



GROUP BY l.loginid
ORDER BY totalScore2 DESC ";

1 个答案:

答案 0 :(得分:0)

这有效:

$resultp = mysql_query($sqlStr5);
$count = 1;


while($rowp = mysql_fetch_array($resultp))
{
    if($u == $rowp['username'])
    {

$points = $rowp['totalScore2'];

    }
    $count++;
}