now() -- 当前时间
curdate() -- 当前日期
date()-- 将时间转化为日期
date_format()-- 将时间转化为特定格式
weekday() -- 取值为0~6,对应星期一到星期日,每周以星期一为第一天
dayofweek() -- 取值为1~7,对应星期日到星期六,每周以星期日为第一天
dayname() -- 取值为Sunday~Saturday,对应星期日到星期六,第一天是星期日;该函数与参数lc_time_names有关,如果设置此参数值为"zh_CN",则得到对应的中文名称“星期日”~“星期六”
例如:
mysql> set lc_time_names='zh_CN';
Query OK, 0 rows affected (0.00 sec)
mysql> select weekday(@day),dayofweek(@day),dayname(@day);
+---------------+-----------------+---------------+
| weekday(@day) | dayofweek(@day) | dayname(@day) |
+---------------+-----------------+---------------+
| 0 | 2 | 星期一 |
+---------------+-----------------+---------------+
参考网址:https://www.jianshu.com/p/0db3fd5df218
#通过身份证获取性别
case
when length(idcard) = 15 and mod(substring(idcard,15,1),2) = 0 then '女'
when length(idcard) = 15 and mod(substring(idcard,15,1),2) = 1 then '男'
when length(idcard) = 18 and MOD(substring(idcard,17,1),2) = 0 then '女'
when length(idcard) = 18 and MOD(substring(idcard,17,1),2) = 1 then '男'
union all # 连接两张表格
例如:每个客户产生n个订单,对每个订单进行排序;
思路:设置两个变量,一个用来计数,一个用来判定是等于上一个用户user_id,若等于,id+1;否则为1;
设置变量技巧:
1、初始化:join (select @area_code := '', @id := 0) as m
2、添加判断条件::@id := if(@area_code = user_id ,@id+1,1) as id
3、每次给变量赋值:@area_code := user_id
select user_id
, gmt_create
, @id := if(@area_code = user_id ,@id+1,1) as id -- 自增判断
, @area_code := user_id -- 每次赋值
from
(
select *
from t_order_info
join (select @area_code := '', @id := 0) as m -- 初始化
group by user_id,gmt_create
) as a