SELECT School_info.district_id,count(School_info.no_of_class) ls
from School_info where school_level IN('ls')
JOIN
SELECT t.district_id,count(t.no_of_class) p from School_info as t
where school_level IN('ps')
ON
( School_info.district_id = t.district_id)
我的错误在哪里,我没找到。当我运行上面的查询时,它显示为: -
MySQL说:文档
1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法 接近' JOIN SELECT t.district_id,count(t.no_of_class)p from School_info as t where sc'在第2行
答案 0 :(得分:1)
您的查询有很多语法错误,但无论如何都可以简化为:
select
district_id
, sum(school_level = 'ls') ls
, sum(school_level = 'ps') p
from school_info
where school_level in ('ls','ps')
group by district_id
sum(school_level = 'p')
使用MySQL对sum()
内的表达式进行布尔评估的事实,如果为真则返回1。对所有1进行求和会得到与计算行相同的结果。
这将为您提供如下输出:
district_id ls p
其中ls
和p
是重要的。