show [global|session] variables like'%time_zone%'; set [global|session|persist] time_zone='+8:00'; flush privileges; # 立即生效
4:自增变量的持久化
Createtabletest1( id int auto_increment primary key, Val int );Insert into test1(val) values (1);Deletefromtablewhere id=1;-- 重启数据库Insert into test1(val) values (1);Select*from test1-- 发现id变成了2,但如果是MySQL5的版本,则id会是1
WithRECURSIVE cte as (select1 n -- SEED查询union allSelect n+1 n from cte where n<8) -- RECURSIVE查询Select*from cte;
以下是2个常用场景
场景1:递归查询指定节点的所有后代
set @customer_id=2; -- 当前节点WITHRECURSIVE cte AS (SELECT a.customer_id, a.inviter_id,cast(a.inviter_id aschar(255)) pathFROM t_customer_relation a WHERE a.customer_id=@customer_idUNION ALLSELECT k.customer_id,k.inviter_id,concat(cte.path,',', k.inviter_id) pathFROM t_customer_relation k INNER JOIN cte ON cte.customer_id = k.inviter_id )SELECT cte.customer_id,cte.inviter_id,cte.path FROM cte -- left join t_customer d on cte.customer_id=d.id
场景2:根据当前节点,查询自己的祖宗
set @customer_id =29 ; -- 当前节点WITHRECURSIVE cte AS(SELECT customer_id, inviter_id, convert(inviter_id , char(255)) pathFROM t_customer_relation WHERE customer_id = @customer_id UNION ALLSELECT c.customer_id, c.inviter_id, concat(cte.path,',', c.inviter_id) pathFROM t_customer_relation c, cte WHERE c.customer_id= cte.inviter_id )SELECT*FROM cte;