简体   繁体   English

连接两个表并显示第一个表中的数据

[英]Joining two tables and display data from first table

I have two tables tblPatient, tblDropDowns tblpatient: 我有两个表tblPatient,tblDropDowns tbl Patient:

firstname   gender  patienttype
anil         1           3
Satheesh     1           4
Vinod        1           4 
Shashikanth  1           3
Srimani      2           3
Thanuja      2           4
Nandini      2           4
Vishu        2           3

and tblDropdowns: 和tblDropdowns:

id  Name
1   Male
2   Female
3   Inpatient
4   Outpatient

Now i want to display the patient table with gender and patient type as their significant names are connected to dropdown table. 现在,我要显示具有性别和患者类型的患者表,因为其重要名称已连接到下拉列表。 result table: 结果表:

firstname          gender       patienttype
anil                male         inpatient
satheesh            male         outpatient
vinod               male          outpatient

please help me out.. thanks anil 请帮帮我..谢谢阿尼尔

In general it would be better avoid storing different things in the same table. 通常,最好避免在同一表中存储不同的内容。 However, you could join with subqueries that only contain the relevant records. 但是,您可以加入仅包含相关记录的子查询。

SELECT firstname, gender.Name AS gender, patienttype.Name As patienttype
FROM tblPatient p
INNER JOIN (SELECT id, Name 
            FROM tblDropdowns
            WHERE id IN (1, 2)) gender 
ON p.gender = gender.id
INNER JOIN (SELECT id, Name
            FROM tblDropdowns
            WHERE id > 2) patienttype
ON p.patienttype = patienttype.id

Please try out this. 请尝试一下。

select [column1], [column2] from tblpatient a, tbldropdowns b
where a.gender = b.id order by a.gender;

You could also use joins: Please refer this W3Schools SQL Link . 您也可以使用联接:请参考此W3Schools SQL链接

Hope this helps. 希望这可以帮助。 Thanks. 谢谢。

Edit Maybe this query could be your solution: 编辑也许此查询可能是您的解决方案:

    select a.firstname, b.name as 'gender', b.name as 'PatientType' 
    from tblpatient a, tbldropdowns b
    where a.gender = b.id and a.patienttype = b.id
    order by a.gender;

Thanks again. 再次感谢。 :-) :-)

尝试如下:

 SELECT firstname, name, CASE WHEN (patienttype=3) THEN 'inpatient' ELSE 'outpatient' END as patienttype_text from tblpatient INNER JOIN tbldropdowns ON gender = id 

join your "tblDropdowns" table twice in your select query. 在您的选择查询中两次将您的“ tblDropdowns”表连接起来。

Please refer this link to understand join, http://www.w3schools.com/sql/sql_join_left.asp 请参考此链接以了解加入, http://www.w3schools.com/sql/sql_join_left.asp

select     tP.firstname,tG.Name gender,tPT.Name patienttype 
from       tblPatient tP 
left join  tblDropDowns tG 
on tG.id  = tP.gender 
left join  tblDropDowns tPT 
on tPT.id = tP.patienttype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM