SQL 语法

1. 流程控制函数

1. case

        ① 使用一:case ... when ... then ... end as ...

                case相当与Java中的 Swith

        语法:

                case 要判断的字段或表达式

                when 常量1 then 要显示的值1或语句1;

                when 常量2 then 要显示的值2或语句2;

                ......

                else 要显示的值n或语句n;

                end

         ②使用二:case when ... then ... end ... as ...

                相当于java中的if..else if..else if..else

                语法:

                case

                when 条件1 then 要显示的值1或语句1

                when 条件2 then 要显示的值2或语句2

                ...

                else 要显示的值n或语句n

                end

2.  时间函数

1.now():返回当前系统日期+时间

select now();

 

2.curdate():返回当前系统日期,不包含时间

select curdate();

 

3.curtime():返回当前时间,不包含日期

select curtime();

4.可以获取指定的部分,年,月,日,小时,分钟,秒

select year(now()) 年;

select year('1998-1-1') 年;

select year(hiredate) 年 from employees;

select month(now()) 月;

select monthname(now()) 月;

5.str_to_date:将日期格式的字符转换成指定格式的日期

STR_TO_DATE('9-13-1999','%m-%d-%Y');----> 1999-09-13

6.date_format:将日期转换成字符

DATE_FORMAT('2018/6/6', '%Y年%m月%d日'); --->2018年06月06日

案例:

str_to_date:

1.select str_to_date('1998-3-2','%Y-%c-%d'); --->1998-03-02

2.查询入职日期为1992-4-3的员工信息

select * from employees where hiredate='1992-4-3'

select * from employees where hiredate=str_to_date('4-3 1992','%c-%d %Y');

date_format:

1.select date(now(), '%y年%m月%d日');

2.查询有奖金的员工名和入职日期(xx月/xx日 xx年)

select last_name, date_format(hiredate, '%m月/%d日 %y年')

from employees

where commission_pct is not null;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值