如何使用左连接创建新表

时间:2014-12-24 18:50:52

标签: php mysql mysqli

我想创建一个新的表调用checkdownload并从两个表中获取数据,这是一个学生和下载表。

   table student

    --------------------------------------------------
    | studentId | username | password | email | name |
    --------------------------------------------------

       table downlaod
    ------------------------------------
    | ID| studentId | downloadId | Name | 
    ------------------------------------

     $sql2="select id from upload";
                $result2=mysqli_query($con,$sql2);
                while($rows2=mysqli_fetch_array($result2))
                {
                    $id5=$rows2['id'];

        $sql3 = "create table checkdownload(SELECT student.studentId, student.name, downlaod.downlaodId, downlaod.Name FROM student as student LEFT JOIN downlaod as downlaod ON student.studentId = downlaod.studentId WHERE downlaod.downlaodId =$id5)";

                    $result3=mysqli_query($con,$sql3);
}

1 个答案:

答案 0 :(得分:0)

CREATE table checkdownload as (SELECT s.studentId, s.name, d.downlaodId, d.Name FROM student s LEFT JOIN downlaod d ON s.studentId = d.studentId WHERE d.downlaodId =$id5)

如果你的查询没问题,这将创建包含结构和所有值的表。如果您只想要没有值的结构,请在最后添加and 1=2