Home > Database > Mysql Tutorial > body text

数据库查询SQL_MySQL

WBOY
Release: 2016-06-01 13:52:18
Original
1156 people have browsed it

1)现在的情况是这样的:有一个表 testtbl( id int, name varchar, level int, age int) ; 根据level的取值分为三个级别:高,中,低。

 现在要统计每个age中,高、中、和低级别的数目。即通过age作group by,但在每个age值内,还要按level取值统计各级别的记录数。

解决方案:

select age, (select count(*) from testtbl t2 where t2.age=t1.age and level )as low,

(select count(*) from testtbl t2 where t2.age=t1.age and level and level >=?)as mid,

(select count(*) from testtbl t2 where t2.age=t1.age and level >?)as high

from testtbl t1 group by age;

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!