我有一个包含100个字段的数据库表,我在计算学生的moy时遇到了问题。
我在使用查询时遇到问题我对所有行都有相同的值。查询是
select
stdt.num as num,
stdt.nom as nom,
(select
(distinct moy_id)
from stdt,
dept,
clas,
moy
where clas.class_id = moy.class_id
and dept.dept_id = stdt.deptid
and moy.value < 10
and moy.value <> 0) as moy,
(select
(distinct moy_id)
from stdt,
dept,
clas,
moy
where clas.class_id = moy.class_id
and dept.dept_id = stdt.deptid
and moy.value <> 10
and moy.value < 4) as moysports
from stdt,
dept,
clas,
moy
where clas.classid = moy.classid
and clas.classid = stdt.clasid
and stdt.stdtid = dept.stdtid
group by stdt.num
计算和条件比这个简单的例子更复杂:我有9个子查询
答案 0 :(得分:0)
select stdt.num as num, stdt.nom as nom, (select (distinct moy_id) from stdt, dept, clas, moy where clas.class_id = moy.class_id and dept.dept_id = stdt.deptid and moy.value < 10 and moy.value <> 0) as moy
这里moy的计算与num和nom无关。
请改为尝试:
select
stdt.num as num,
stdt.nom as nom,
moy_id as moy
from stdt,
dept,
clas,
moy
where clas.classid = moy.classid
and clas.classid = stdt.clasid
and stdt.stdtid = dept.stdtid
and moy.value between 1 and 10
我无法理解为什么你想在那里分组,所以我把它放在那里。由你来重新添加。
答案 1 :(得分:0)
你加入了什么部门?你加入clas的是什么?如果学生有多个匹配的moy_id,你想要展示哪一个? min或max或group_concat?这是简化的陈述。
select
stdt.num as num,
stdt.nom as nom,
min
((
select moy_id
from moy
where moy.classid = stdt.classid
and moy.value < 10
and moy.value <> 0
)) as moy_id
from stdt;