子查询中的mySQL错误1064

时间:2015-12-20 15:11:38

标签: mysql subquery

我知道这可能很愚蠢,但我花了2个小时寻找错误,我的截止日期不多了。你能帮忙吗?

这是代码

create view Children as
select avg(chAvg) as Avg_Children
from
(select CHILDREN.ID,
CHILDREN.PAT_ID,
CHILDREN.GENDER,
DRUGS.DRUG_TYPE,
count(CHILDREN.ID)/count(distinct(CHILDREN.PAT_ID)) as 'chAvg'
from CHILDREN
join PATIENTS
on CHILDREN.PAT_ID = PATIENTS.PATIENT_ID
join CASES
on PATIENTS.PATIENT_ID=CASES.PAT_ID
join DRUGS_TO_CASES
on CASES.CASE_ID = DRUGS_TO_CASES.CASE_ID
join DRUGS
on DRUGS_TO_CASES.DRUG_ID=DRUGS.ID
where DRUGS.DRUG_TYPE = 'tranquillisers'
group by CHILDREN.GENDER)

这就是Workbench所说的:

Error Code: 1064. 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 '' at line 19

1 个答案:

答案 0 :(得分:1)

在子查询上使用别名,并尝试避免使用与表相同的名称,即使mysql允许它

create view VChildren as
select avg(chAvg) as Avg_Children
from
(select CHILDREN.ID,
CHILDREN.PAT_ID,
CHILDREN.GENDER,
DRUGS.DRUG_TYPE,
count(CHILDREN.ID)/count(distinct(CHILDREN.PAT_ID)) as 'chAvg'
from CHILDREN
join PATIENTS
on CHILDREN.PAT_ID = PATIENTS.PATIENT_ID
join CASES
on PATIENTS.PATIENT_ID=CASES.PAT_ID
join DRUGS_TO_CASES
on CASES.CASE_ID = DRUGS_TO_CASES.CASE_ID
join DRUGS
on DRUGS_TO_CASES.DRUG_ID=DRUGS.ID
where DRUGS.DRUG_TYPE = 'tranquillisers'
group by CHILDREN.GENDER)x