开发者

Creating A New Variable Equal To The Result Of A Join Query

I am running the query below on a page where there is a session variable called $u. I wou开发者_运维问答ld like to create a variable called $points equal to totalScore2 from the query below where l.username equals $u. The field l.username will equal $u only once.

How can I do this?

Thanks in advance,

John

$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 ";


This worked:

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


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

$points = $rowp['totalScore2'];

    }
    $count++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜