在mysql有个表user,
         id         name         age
          1         tonny         18
          1         wendy         30
          1         james         25
          1         simon         22
          1         flynn         35
          1         nancy         42
          1         olivia        51
          1         linda         20我想统计出不同年龄段的人数,
     年龄段           人数
     18-20             2
     21-30             3
     31-40             1
     41-50             1
     >50               1
请问上面的数据 语句该怎么写,请高手指教,不胜感激,多谢!!!

解决方案 »

  1.   


    select count (1) AS 'No','18-20' as 'age'
    into #A 
    from mytest 
    where age in (18,19,20)select count (1) AS 'No','21-30' as 'age'
    into #B 
    from mytest 
    where age in (18,19,20)select count (1) AS 'No','31-40' as 'age'
    into #C 
    from mytest 
    where age in (18,19,20)select count (1) AS 'No','41-50' as 'age'
    into #D 
    from mytest 
    where age in (18,19,20)select count (1) AS 'No','UP 50' as 'age'
    into #E 
    from mytest 
    where age > 50SELECT * FROM #A,#B,#C,#D,#Echange table name whatever you like .
      

  2.   

    this SP has run through ,you just need to change the table name .if you want to change the column name to birthdate ,you can use the same way to do this job .good luck !