获取查询中未说明的用户

时间:2012-08-05 12:44:21

标签: php sql sql-server

我收到了这个问题:

$characterinfoquery = "
SELECT 
    c.Name, 
    c.Level, 
    c.Sex, 
    c.Playtime, 
    c.KillCount, 
    c.DeathCount,
    cl.Name AS clanName
FROM 
    Character AS c, 
    Account AS a,
    ClanMember AS cm,
    Clan AS cl
WHERE 
    c.AccountID = a.AccountID AND
    c.CharacterID = cm.CharacterID AND
    cm.ClanID = cl.ClanID AND
    a.UserID='".mssql_real_escape_string($_SESSION['username'])."'
";

但是我希望那些没有战队的成员也能被展示出来,而不是部落名称,它会在部落名称所在的地方说“ - ”。

这是我的陈述:

if(mssql_num_rows($characterinforesult) != 0){
    $content = str_replace("%content%", file_get_contents("tpl/contents/characterinfo.html"), $content);

    //Get character information
    $search = array("%Name%", "%Level%", "%Sex%", "%Playtime%", "%KillDeath%", "%Clan%");
    $rows = file_get_contents("tpl/contents/characterinfo_tr.html");
    while($row = mssql_fetch_assoc($characterinforesult)){

        if($row['KillCount'] != 0){
            $KillDeath = round($row['KillCount']/$row['DeathCount'], 2);
        }
        else{
            $KillDeath = "-";
        }
        $Playtime = $row['Playtime']/60;
        $replace = array($row['Name'], $row['Level'], gender($row['Sex']), round($Playtime), $KillDeath, $row['clanName']);
        $tr .= str_replace($search, $replace, $rows);
    }
}

有人可以帮我这个吗?

使用innerjoins输出:

Name    Level   Sex     Playtime    K/D Ratio   Clan
DragonDex   97  Male    375 min     0.22            Test

它显示1行,而该帐户中有2个字符,1个有一个部落而另一个没有。

1 个答案:

答案 0 :(得分:3)

您是否需要左外连接:

SELECT 
    c.Name, 
    c.Level, 
    c.Sex, 
    c.Playtime, 
    c.KillCount, 
    c.DeathCount,
    coalesce( cl.Name, ' - ' ) AS clanName
FROM 
    Character AS c 
          inner join  
    Account AS a 
          on c.AccountID = a.AccountID
          left outer join 
    ClanMember AS cm 
          on c.CharacterID = cm.CharacterID
          left outer join 
    Clan AS cl
          on cm.ClanID = cl.ClanID
WHERE 
    a.UserID='".mssq ...