• 常用的一些复杂SQL语句


    1.根据表中的birthday统计年龄段人数:

    //以下代码表示查询出来后的结果集添加一列字段

    cast('20以下' as char) as age
    SELECT COUNT((DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y'))) as value,cast('20岁以下' as char) as name 
                FROM hm_applicant WHERE (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) <= 20
    UNION
    SELECT COUNT((DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y'))) as value,cast('20-40岁' as char) as name 
                FROM hm_applicant WHERE (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) > 20 AND (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) <= 40
    UNION
    SELECT COUNT((DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y'))) as value,cast('40-60岁' as char) as name  
            FROM hm_applicant WHERE (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) > 40 AND (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) <= 60
    UNION
    SELECT COUNT((DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y'))) as value,cast('60-80岁' as char) as name 
            FROM hm_applicant WHERE (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) > 60 AND (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) <= 80
    UNION
    SELECT COUNT((DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y'))) as value,cast('80岁以上' as char) as name 
        FROM hm_applicant WHERE (DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(birthday,'%Y')) > 80

    效果图如下图:

  • 相关阅读:
    spring data jpa @index无法创建索引
    vue form绑定数据后只能显示不能编辑
    vue dialog每次打开会展示上一次数据(转载)
    vue-i18n突然所有的都解析不出来了
    sass计算高度
    动态指定日志路径(logback)
    Springboot配置文件内容加密
    java环境变量修改后不生效
    SOAP和REST
    Git配置别名
  • 原文地址:https://www.cnblogs.com/dreamydeng/p/5742485.html
Copyright © 2020-2023  润新知