我必须关注表格,我想加入它们,其中icd9中的序列为1,每个主题在入学表中都有一个hadm_id。每个主题可以有多个hadm_id,但我想要那些只有1个hadm_id的人。我也想要序列值为1的主题。
icd9
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| subject_id | int(11) | NO | MUL | NULL | |
| hadm_id | int(11) | NO | MUL | NULL | |
| sequence | int(11) | NO | | NULL | |
| code | varchar(100) | NO | | NULL | |
| description | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
admissions
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| hadm_id | int(11) | NO | PRI | NULL | |
| subject_id | int(11) | NO | MUL | NULL | |
| admit_dt | datetime | NO | | NULL | |
| disch_dt | datetime | NO | | NULL | |
+------------+----------+------+-----+---------+-------+
我的查询如下。当我运行它时,我得到以下错误
select * from icd9
where sequence=1 as t1
inner join
(select * from
(select subject_id, count(hadm_id)
as n_admissions from admissions
group by subject_id
) as q1
where n_admissions = 1
order by subject_id limit 10) as q2
AS t2
ON t1.subject_id = q2.subject_id;
我不确定如何解决此错误。我尝试了几种方法,但我一直都这样做。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as t1
inner join
(select * from
(select subject_id, count(hadm_id)
' at line 2
答案 0 :(得分:1)
试试这个:
select *
from icd9 as t1
inner join
(
select subject_id
from admissions
group by subject_id
having count(hadm_id) = 1
order by subject_id
limit 10
) as q2
ON t1.subject_id = q2.subject_id and t1.sequence = 1;
这样,您正在进行分组,使用HAVING进行过滤,在一个子查询中进行排序和限制,然后加入icd9。