我有2个表病史和病史中的患者我有字段名称DoctorId和PatientId,在患者中我有PatientId,FirstName,MiddleName,LastName
到目前为止,我的查询是它只过滤一张表和来自病史的患者 ID。
$dids = $_SESSION["DoctorId"];
if(isset($_POST["btnsrch"])&& ($_POST["textbox"]!="")){
$pid = $_POST["textbox"];
$pid= mysqli_real_escape_string($_POST["textbox"]);
$query2= mysqli_query($link,"Select DISTINCT PatientId from medicalhistory where DoctorId='". $dids ."' or PatientId='". $pid ."'" );
}
我的问题是我将如何查询名字、中间名和姓氏,但它只会查询在病史中有该医生记录的姓名?
最佳答案
$query2= mysqli_query($link,"SELECT p.PatientId,p.firstname,p.middlename,p.lastname
FROM patient p
INNER JOIN medicalhistory as pa ON (p.PatientId = pa.PatientId)
WHERE pa.PatientId='".$pid."'" );
这将是您修改后的代码:
$dids = $_SESSION["DoctorId"];
if(isset($_POST["btnsrch"])&& ($_POST["textbox"]!="")){
$pid = $_POST["textbox"];
$pid= mysqli_real_escape_string($_POST["textbox"]);
$query2= mysqli_query($link,"SELECT p.PatientId, p.FirstName, p.MiddleName, p.LastName,pa.doctorID,pa.Evaluation FROM patient p INNER JOIN medicalhistory as pa ON (p.PatientId=pa.PatientId) WHERE pa.PatientId='".$pid."'" );
}
示例输出:
mysql> select p.id,p.First,p.last, p.middle,pa.id,pa.evalution from test1 as p left join test as pa on (p.id=pa.id) where p.first='Dan' and p.id=4 and p.last='smi
th';
+----+-------+-------+----------+----+-----------+
| id | First | last | middle | id | evalution |
+----+-------+-------+----------+----+-----------+
| 4 | Dan | Smith | chandran | 4 | sick |
+----+-------+-------+----------+----+-----------+
1 row in set (0.00 sec)
关于php - 如何在多个表中进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477134/