繁体   English   中英

我可以合并这两个选择查询吗?

[英]Could i merge this two select query in one?

我的目标是在正确的部分显示/显示调查问题(从我的数据库表中),在左侧部分显示/显示客户的答案(来自我数据库中的另一个表)。 所以我的问题是:如何合并这两个选择查询? 我做了一些研究但是使用php它有点难以理解,而且我在php上也是新手。 欢迎任何帮助或建议。

最好的问候AV

<?php
    include("bdconnect_Foredeck.php");
    $link=mysqli_connect($host,$login,$pass,$dbname);

    if(isset($_POST["bouton55"])){
        $link = mysqli_connect($host,$login,$pass,$dbname);  

        $id = $_REQUEST["Zoubi"];
        $ClientRef =$_REQUEST["KGB"];

        $rechercheq = "SELECT Qref,Ref,Question FROM questionnaire WHERE Qref ='$id' ";
        $recherche= "SELECT choix,commentaire FROM reponse WHERE RefQ ='$id' and ref_Client ='$ClientRef'";

        mysqli_query($link,$recherche);
        mysqli_query($link,$rechercheq);

        $result1=mysqli_query($link,$rechercheq); 
        $result= mysqli_query($link,$recherche);

        while($row = mysqli_fetch_assoc($result,$result1)){
            $Ref =$row["Ref"];
            $Question       =$row["Question"];
            $Choix =$row["choix"];
            $Commentara =$row["commentaire"];

            echo" <tr bgcolor=\"white\">
            <td>  $id  </td>
            <td> $Ref </td>
            <td>$Question </td>
            <td>$Choix       </td>
            <td>$Commentara         </td>
            </tr>";
        }
    }
?>

你可以使用JOIN

    SELECT a.Qref, a.Ref,a.Question , b.choix, b.commentaire
    FROM questionnaire as a 
    LEFT JOIN reponse as b ON  a.RefQ = b.RefQ
    WHERE a.Qref ='$id' 
    AND b.ref_Client ='$ClientRef'

如果你有重复的行...那么你可以使用distinct

    SELECT DISTINCT a.Qref, a.Ref,a.Question , b.choix, b.commentaire
    FROM questionnaire as a 
    LEFT JOIN reponse as b ON  a.RefQ = b.RefQ
    WHERE a.Qref ='$id' 
    AND b.ref_Client ='$ClientRef'

否则你的逻辑不允许单个查询

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM