sql —— 函数及关键字

1.distinct        ——消除相同数据

//显示表名为tableName的表中的field字段,相同数据仅显示一次
select distinct fieldName from tableName;

   

2.group by        ——根据group by 后面的字段进行分类汇总

3.having           ——对group by聚合后的结果在进行一次挑选

//将表名为tableName的表根据字段fieldName进行分组聚合并对每组进行计数(count(fieldName)),
//输出其中计数大于1的数据的fieldName字段及其计数结果
//计数结果显示在新的名称为num的字段下(不会影响数据库的数据)
select fieldName,count(fieldName) as num 
from tableName 
group by    fieldName
having     num>1; 

3.datadiff              ——求日期之间的差值

// datadiff(date1,date2)求date1和date2之间的差值(date1-date2)。
//求名为tableName1的表和名为tableName2的表的交叉连接中 date1 和date2 相差1的数据
//若是整型数据,等价于 where a.date1-b.date2=1

select *  from tableName1 as a, tableName2 as b
where datediff(a.date1,b.date2)=1;

4.union                ——会对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序。

//在表名为tableName的表中筛选 a>0 和 b>0 的数据
select * from tableName
where a>0

union

select * from tableName
where b>0

5.case...when...end        ——sql 中的选择语句

        (1)简单case函数

//未验证 
case sex
when '1' then '男'
when '0' then '女'
else  '错误'
end

        (2)case 搜索函数

case 
when sex='1' then '男'
when sex='0' then '女'
else '错误'
end
 

例子:610. 判断三角形 - 力扣(LeetCode)

#判断x,y,z是否能组成三角形
#表 triangle 中有三个字段,x,y,z,且取值都为整数,判断x、y、z是否能组成三角形并按下行输出
#输出:x y z triangle(即判断结果)
select x,y,z,(case
    when x+y>z and x+z>y and y+z>x then 'Yes'
    else 'No'
    end) as triangle
from triangle

6.if(expression,trueThen,falseThen)

count(if(expression,1,num))
sum(if(expression,1,0))
sum(if(expression,column,0))

例子:1211. 查询结果的质量和占比 - 力扣(LeetCode)

# Write your MySQL query statement below
select query_name,round(sum(rating/position)/count(*),2) as quality,round(count(if(rating<3,1,null))/count(*)*100,2) as poor_query_percentage
from (select distinct query_name,result,position,rating 
    from queries) as a
group by query_name

7.IFNULL(exp1,exp2)——当 exp1 为 null 时,变更为 exp2。

ifnull(sum(column),0)    #当column 列求和结果为 null 时,变更为 0

例子:1407. 排名靠前的旅行者 - 力扣(LeetCode)

# Write your MySQL query statement below
select  a.name,ifnull(sum(b.distance),0) as travelled_distance
from users as a left join Rides as b on b.user_id=a.id
group by b.user_id 
order by travelled_distance desc, name

8. group_concat  ——将组中字符串以指定分隔符连接成单个字符串,不指定时默认为','

group_concat(distinct expression    #去重
            order by expression    #排列顺序
            separator sep);        #分隔符    eg.','

例子:1484. 按日期分组销售产品 - 力扣(LeetCode)

# Write your MySQL query statement below
select  sell_date,
        count( distinct product) as num_sold,
        group_concat(distinct product
                    order by product   
                    separator ",") as products
from    Activities
group by sell_date
order by sell_date;

9.concat(str1,str2...,strn)        按序连接字符串 str1 str2 ......strn ,形成新的字符串

10.length(str1)        获得字符串 str1 的长度

11.upper(str1)        将 str1 中的字母字符转换为大写 

12.lower(str1)        将 str1 中的字母字符转换为小写

13.substring(str1,x,y)        截取 str1 从位置 x 开始的 y 个字符串作为字符串,注意,sql 中字符串位置从 1 开始。

       substring(str1,x)        截取 str1 从位置 x 开始的所有字符

            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值