如何计算行中的datediff

时间:2015-03-16 14:36:24

标签: mysql sql

表格患者

enter image description here

downarrow

表格录入

enter image description here

表格tbl_master

enter image description here

downarrow

tbl_action

enter image description here

我需要计算不同的时间fibrinolytic_datetime - admission_datetime和table admission field Fibrinolytic是SK。

我试试

SELECT   
       admission.AN,
       admission.HN,
       pat_name,
       pat_lname,
       Fibrinolytic,
       DATEDIFF(date_time,(SELECT date_time from tbl_action WHERE tbl_id = "tbl001"))
FROM patient,admission,tbl_action
WHERE admission.HN = patient.HN AND admission.AN = tbl_action.AN
AND Fibrinolytic = "SK" AND tbl_id = "tbl002"

但来自phpmyadmin的回复"Subquery returns more than 1 row"

我想要总计 enter image description here

1 个答案:

答案 0 :(得分:0)

Inner Join而不是Cross Join

加入您的表格

您的子查询返回的值超过1,因为表tbl_action中有3行对tbl_Id = "tbl001"

SELECT date_time from tbl_action WHERE tbl_id = "tbl001"

您必须通过在此查询的where子句中添加一些唯一ID来唯一标识您要查找的date_time

SELECT date_time from tbl_action WHERE tbl_id = "tbl001" and AN ="AN001"

修改

SELECT   
       admission.AN,
       admission.HN,
       pat_name,
       pat_lname,
       Fibrinolytic,
       DATEDIFF(date_time,(SELECT date_time from tbl_action WHERE tbl_id = "tbl001" and AN ="AN001"))
FROM patient
inner join admission on admission.HN = patient.HN 
inner join tbl_action on admission.AN = tbl_action.AN
WHERE  admission.Fibrinolytic = "SK" AND tbl_action.tbl_id = "tbl002"