SQL内连接

时间:2013-03-31 02:14:33

标签: php sql inner-join

我需要使用内部联接显示来自另一个表的数据。

$pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
    SELECT Doctor_ID
    FROM Appointment
    INNER JOIN Doctor
    ON Appointment.Doctor_id=Doctor.Doctor_id

目前我从一个表中显示数据,但我还需要使用内部联接显示另一个表中的数据。如何将创建的SELECT代码插入到当前编码中?我正在尝试使用医生ID在我的页面中输出医生的详细信息。我是php的新手。

谢谢

appointment.php的完整php代码

<?php
{
    mysql_connect("localhost" , "" , "") or die (mysql_error());
    mysql_select_db("") or die(mysql_error());


    $pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_date FROM Appointment where Patient_id=$pid";
    SELECT Doctor_id
    FROM Appointment
    INNER JOIN Doctor
    ON Appointment.Doctor_id=Doctor.Doctor_id

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1) {}
    {
        $row=mysql_fetch_array($result);
        $_SESSION['Appointment_date'] = $row['Appointment_date'];
        $_SESSION['Appointment_time'] = $row['Appointment_time'];

    }
}
?>  

        <strong>Dates available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select date--</option>
        <option value="1"><?php echo $_SESSION['Appointment_date'];?></option>
        </select>

        <br />
        <br />

        <strong>Times available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select time--</option>
        <option value="2"><?php echo $_SESSION['Appointment_time'];?></option>>
        </select>

第57至98行的完整代码

1 个答案:

答案 0 :(得分:0)

我同意这些意见。试试这个(新的查询语法......)

<?php
{
mysql_connect("localhost" , "" , "") or die (mysql_error());
mysql_select_db("") or die(mysql_error());


$pid=intval($_SESSION["Patient_id"]); 
$query = "SELECT Apointment.Apointment_id, Apointment.Doctor_id, Apointment.Patient_id, Apointment.Time
          FROM (Doctor INNER JOIN Apointment ON Doctor.Doctor_id = Apointment.Doctor_id) INNER JOIN Patient ON Apointment.Patient_id = Patient.Patient_id
          WHERE Apointment.Patient_id='". $pid . "'";

//executes query on the database
$result = mysql_query ($query) or die ("didn't query");
//this selects the results as rows

$num = mysql_num_rows ($result);
//if there is only 1 result returned than the data is ok 
if ($num == 1) {}
{
$row=mysql_fetch_array($result);
$_SESSION['Appointment_date'] = $row['Appointment_date'];
$_SESSION['Appointment_time'] = $row['Appointment_time'];

}
}
?>