zoukankan      html  css  js  c++  java
  • SQL之Case when 语句

      --case简单函数  (把多列变成单列)
     select case sex when '0' then '' 
            when '1' then '' else '其他' end from [Northwind].[dbo].[Users]
      --case搜索函数
      select case when sex='0' then ''
              when sex='1' then '' 
              else '其他' end  from [Northwind].[dbo].[Users]

      从两种写法就可以看出,第一种太局限了,一般满足不了我们的复杂条件,我们的搜索函数相比就比较好了,还有一个需要注意的问题,Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。 

    group by 用法  这是最常见的用法

       select id,
        count(case when sex='0' then '' end) 女生数,
        COUNT(case  when sex='1' then '' end) 男生数 from [Northwind].[dbo].[Users]   group by id

    count求和

       select 
        count(case when sex='0' then '' end) 女生数,
        COUNT(case  when sex='1' then '' end) 男生数 from [Northwind].[dbo].[Users]   

  • 相关阅读:
    Qt之Threads和QObjects
    Qt之可重入与线程安全
    Qt之线程基础
    Qt之QLineEdit
    Qt之属性系统
    Django框架
    web框架起源
    django查看数据库
    jQuery
    BOM&DOM
  • 原文地址:https://www.cnblogs.com/Sea1ee/p/6688892.html
Copyright © 2011-2022 走看看