如何从有条件的表中选择所有数据?

编程入门 行业动态 更新时间:2024-10-23 21:27:53
本文介绍了如何从有条件的表中选择所有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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.

更多推荐

如何从有条件的表中选择所有数据?

本文发布于:2023-10-23 06:00:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!