内/左连接有两个不同的where子句

时间:2014-01-02 00:31:10

标签: php mysql sql mysqli

我正在两个不同的条件下将两张桌子连在一起。对于主要示例,假设我有以下嵌套查询:

$Query = $DB->prepare("SELECT ID, Name FROM modifications
WHERE TYPE =1 & WFAbility = '0'");
$Query->execute();
$Query->bind_result($Mod_ID,$Mod_Name); 

和这个查询:

$Query= $DB->prepare("SELECT `ModID` from `wfabilities` WHERE `WFID`=?");
$Query->bind_param();
$Query->execute();
$Query->bind_result();
while ($Query->fetch()){  }

基本上,我想选择type等于1的所有元素,而Ability等于0,这是从修改表中选择的。

我还需要从wfabilities中选择所有ID,但是将它们转换为WFID等于另一个查询结果的修改中的名称。

这是我目前的半工作代码。

$Get_ID = $DB->prepare("SELECT ID FROM warframes WHERE Name=?");
        $Get_ID->bind_param('s',$_GET['Frame']);
        $Get_ID->execute();
        $Get_ID->bind_result($FrameID);
        $Get_ID->fetch();
        $Get_ID->close();
            echo $FrameID;
            $WF_Abilties = $DB->prepare("SELECT ModID FROM `wfabilities` WHERE WFID=?");
            $WF_Abilties->bind_param('i',$FrameID);
            $WF_Abilties->execute();
            $WF_Abilties->bind_result($ModID);
                $Mod_IDArr = array();
            while ($WF_Abilties->fetch()){
                $Mod_IDArr[] = $ModID;
            }
            print_r($Mod_IDArr);
                $Ability_Name = array();
                foreach ($Mod_IDArr AS $AbilityMods){
                    $WF_AbName = $DB->prepare("SELECT `Name` FROM `modifications` WHERE ID=?");
                    $WF_AbName->bind_param('i',$AbilityMods);
                    $WF_AbName->execute();
                    $WF_AbName->bind_result($Mod_Name);
                    $WF_AbName->fetch();
                        $Ability_Name[] = $Mod_Name;
                }
            print_r($Ability_Name);

3 个答案:

答案 0 :(得分:1)

见下文:

SELECT ModID, 
     ID,
     Name
FROM modifications M    
LEFT JOIN wfabilities WF
    ON WF.ModID = M.ID
WHERE TYPE =1 & WFAbility = '0'

答案 1 :(得分:1)

要做到这一点,你需要加入你的桌子,我不太确定你要做什么,所以你可能需要给我更多的信息,但这是我的猜测。

SELECT ID, Name, ModID 
FROM modifications
JOIN wfabilities 
  ON WFID = ID
WHERE TYPE = '1' 
  AND WFAbility = '0'

在这个版本中,如果ID为WFID,则我连接表。你必须告诉我究竟是什么应该挂钩你的要求。

要详细了解联接及其功能,请查看此页:MySQL Join

编辑:

在查看较大的结构后,我可以看到你可以这样做:

SELECT modifications.Name FROM modifications
JOIN wfabilities on wfabilities.ModID = modifications.ID
JOIN warframes on warframes.ID = wfabilities.WFID
WHERE warframes.Name = 'the name you want'

此查询将为您提供warframes名称中的ability_names数组。

答案 2 :(得分:0)

这是查询:

"SELECT A.ID, A.Name,B.ModID,C.Name
FROM modifications as A 
LEFT JOIN wfabilities as B ON A.ID = B.WFID 
LEFT JOIN warframes as C ON C.ID = B.WFID 
WHERE A.TYPE =1 AND A.WFAbility = '0' AND C.Name = ?"