SQL基础查漏补缺

SQL基础查漏补缺

  • 去重:select distinct xxx from xxx

  • "+"号在mysql中只有一个功能:运算符

    select 100+90 # 190
    select '123' + 90 # 213
    select 'john' + 90 # 90
    select null + 90 # null
    
  • 字符串拼接concat

    select concat('a','b','c') #abc
    
  • 模糊查询like、between and、in、is null

    select * from xxx where xxx=null # "="匹配不了null 需要使用is null
    # 安全等于 <=>:可以匹配null,但是可读性低
    
  • 常见函数

    #1.单行函数
    #1.1字符函数(length获取长度:字节个数)
    select length('john') # 4
    select lengtj("哈哈哈") # 9(utf-8:一个汉字三个字节,gbk:一个汉字2个字节)
    #1.2数学函数(concat)
    #1.3 upper lower
    select upper('john') # JOHN
    select concat(upper('j'),lower('ohn')) # John
    #1.4 截取字符
    select substr('李莫愁爱上了陆湛远',7) # sql中索引从1开始,陆湛远
    select substr('李莫愁爱上了陆湛远',7,3) # 第三个参数为长度,陆湛远
    select concat(upper(substr('john',1,1)),lower(substr('john',2)))
    #1.5 instr
    select instr('杨不悔爱上了殷六侠','殷六侠') # 7,返回字串第一次出现的起始索引,0就是未出现
    select trim('    lll      ') #lll,去掉前后空格
    select trim('a' from 'aaaaaalllaaaaallllaaaaaa') # lllaaaaallll
    #1.6 lpad、rpad
    select lpad('殷素素',4,'*') # *殷素素,用指定字符左填充,4代表总长度
    select lpad('殷素素',2,'*') # 殷素
    #1.7 replace替换
    select replace('周芷若周芷若爱上了张无忌','周芷若','赵敏') #赵敏赵敏爱上了张无忌
    #2.数学函数
    #2.1 round 四舍五入
    select round(1.65) #2
    select round(-1.557,2)# -1.56
    # 2.2 ceil >= 向上取整 floor <= 向下取整
    # 2.3 truncate 截断
    select truncate(1.69999,1) # 1.6,截断1.69999小数点后一位
    # 2.4 mod 取余
    select mod(10,3) # 1,与 select 10%3 一致
    select mod(-10,3) # -1, a-a/b*b
    #3.日期函数
    #3.1 now 当前系统日期+时间
    #3.2 curdate 当前系统日期,不包含时间
    #3.3 curtime 当前时间,不包含日期
    #3.3 获取指定的部分year、month
    select year(now())
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值