我是 Android 编程新手,在查询方面遇到问题。我尝试搜索类似的问题,但什么也没找到:)所以,在这里..
给定这个患者信息表
patient_id | name | gender | age
1 | jen | female | 20
2 | jay | male | 19
和记录表
patient_id | date | description
1 | 01-01-14 | healthy!
1 | 02-01-14 | healthy!
2 | 01-01-14 | needs medication.
我想获取某个患者的所有记录。 例如,如果我选择病人1..将显示病人1的所有记录。
这是我的查询..
$query = "SELECT * FROM records INNER JOIN patientinfo ON records.patient_id = patientinfo.patient_id WHERE records.patient_id = $patient_id";
try{
$stmt = $dbname->prepare($query);
$result = $stmt->execute();
}catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = $ex;
die(json_encode($response));
}
但它给了我一个sql语法错误,我认为它在$patent_id
errorInfo: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use.
提前谢谢您!希望得到答复!谢谢:)
最佳答案
尝试方法:
SELECT *
FROM patientinfo, records
WHERE (patientinfo.patient_id = $patient_id) AND (patientinfo.patient_id = records.patient_id);
您还可以专门列出要显示的列,并为它们分配一个别名,例如:
SELECT records.date AS the_date
只要 $ Patient_id 不是空字符串,WHERErecords. Patient_id = $Patent_id
似乎就可以。为此,我会在执行查询并检查它是否正常之前回显
查询。
关于php - 如何从有条件的表中选择所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611818/