MySql常用查询语句中函数的使用

1.判断表内字段是否为空,若空则自定义:

select IFNULL(name,'user') credit from student

2.获取系统当前时间(具体时间形式请参阅:http://www.w3school.com.cn/sql/sql_dates.asp

select CURDATE() from student

3.将表内时间替换为指定格式时间:

select DATE_FORMAT(bdi.activedate,'%Y-%m-%d') from base_dealer_info bdi

4.做简单的判断统计(分别统计出表内各status的数量以及reporttype数量):

select count(*) as num,sum(case when bdi.status = 4 then 1 else 0 end) as status1 ,
    sum(case when bdi.status = 4 then 1 else 0 end) as status2 ,
    sum(case when bdi.status = 4 then 1 else 0 end) as status3 ,
    sum(case when bdi.status = 4 then 1 else 0 end) as status4 ,
    sum(case when bdi.reporttype = 1 then 1 else 0 end) as reporttype1 ,
    sum(case when bdi.reporttype = 2 then 1 else 0 end) as reporttype2 ,
    sum(case when bdi.reporttype = 3 then 1 else 0 end) as reporttype3 
    from base_dealer_info bdi
  inner join base_org_dealer_ref bodr on bdi.enterpriseid=bodr.dealerid 
  where orgcode='welink' and projectcode='pro001'

5.向下递归函数的编写

BEGIN   
DECLARE str varchar(2000);  
DECLARE cid varchar(100); 
DECLARE s BOOLEAN;
SET s = true;  
SET str = '$';   
SET cid = rootId;   
WHILE cid is not null DO  
        IF s THEN
            SET str = cid;
            SET s = false;
        ELSE
            SET str = concat(str, ',', cid);   
        END IF;
    SELECT group_concat(enterpriseid) INTO cid FROM base_enterprise_ref where FIND_IN_SET(parentid, cid) > 0;   
END WHILE;   
RETURN str;   
END

表结构

参数定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值