Sinan Samet
Sinan Samet

Reputation: 6752

Get users which are not stated in the query

Well I have gotten this query:

$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'])."'
";

But I want the members who do not have a clan to be showed too but instead of the clan name it would say "-" at where the clan name is supposed to be.

This is my while statement:

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);
    }
}

Could someone help me with this?

Output with innerjoins:

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

It shows 1 row while there are 2 characters in that account, 1 has a clan the other doesn't.

Upvotes: 0

Views: 67

Answers (1)

dani herrera
dani herrera

Reputation: 51665

Do you need a left outer join:

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 ...

Upvotes: 3

Related Questions