我如何在查询$ r2中使用变量表$ r1而不是php文件中的FROM子句上的表名

时间:2017-04-07 10:33:01

标签: php mysql sql-server

$r1 = mysqli_query($con,"SELECT course_id,status FROM attendance WHERE stud_id = '$stud_id'");

$r2 = mysqli_query($con,"SELECT course_name,status FROM **$r1** NATURAL JOIN course WHERE stud_id = '$stud_id'");

2 个答案:

答案 0 :(得分:0)

您可能必须使用subquery

$r2 = mysqli_query($con,"SELECT course_name,status FROM 
        (SELECT course_id,status FROM attendance WHERE stud_id = '$stud_id') 
        NATURAL JOIN course WHERE stud_id = '$stud_id'");

答案 1 :(得分:0)

首先,永远不要使用没有正确转义的变量(mysqli::real-escape-string

你可以加入两个这样的表:

$r2 = mysqli_query(
    $con,
    "SELECT c.course_name, a.status FROM attendance a
    INNER JOIN course c ON c.stud_id = a.stud_id 
    WHERE stud_id = '$stud_id'"
);