如何从有条件的表中选择所有数据? [英] How can I select all the data from a table with condition?

查看:46
本文介绍了如何从有条件的表中选择所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android编程的新手,但查询遇到问题.我试图搜索类似的问题,但什么都没找到:)所以,在这里.

I am new to android programming and I am having trouble with queries. I tried to search for similar questions but I found nothing :) so, here..

提供了此PatientInfo表

given this patientinfo table

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.

我想获取某个患者的所有记录. 例如,如果我选择Patient1 ..,则会显示Patient1的所有记录.

I'd like to get all the records of a certain patient. like for example, if I choose patient1.. all records of patient1 will be displayed.

这是我的查询.

$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语法错误,我认为它在$patient_id


but it gives me an error in sql syntax, I think it's in the $patient_id

errorInfo::您的SQL语法有错误;检查手册 对应于您使用正确语法的MySQL服务器版本.

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.

先谢谢您!希望得到答案!谢谢:)

Thank you in advance! hoping for an answer! Thanks :)

推荐答案

尝试者:

SELECT *
FROM patientinfo, records
WHERE (patientinfo.patient_id = $patient_id) AND (patientinfo.patient_id = records.patient_id);

您还可以专门列出要显示的列,并为其指定别名,例如:

You may also specifically list the columns to show, and assign them an alias, for instance:

SELECT records.date AS the_date

只要$ Patient_id不是一个空字符串,WHERE records.patient_id = $patient_id似乎还可以.为此,在执行查询之前,我会先echo查询并检查它是否还可以.

The WHERE records.patient_id = $patient_id seems ok as long as $patient_id is not an empty string. For that, I'd echo the query prior to executing it and checking whether it's ok.

这篇关于如何从有条件的表中选择所有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆