【SQL】Mysql常用sql语句记录

1、创建用户、赋予权限

CREATE DATABASE scadm DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'scadm' IDENTIFIED BY 'scadm';
GRANT ALL ON `db1`.* TO 'scadm';
GRANT ALL ON `db2`.* TO 'scadm';
GRANT ALL ON `db3`.* TO 'scadm';

revoke all on db3.* from scadm;  //撤销权限

2、时区设置

show variables like '%time_zone%';
set global time_zone='+8:00';

3、mysql 更新最新的一条记录 

 update tb_test set name='小东'  where type=2 order by created_at DESC limit 1;

 4、分组取每组前几条记录

--[比当前记录val大的条数]小于2条;即当前记录为为分组中的前两条
--方法一   select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val;
--方法二   select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val;
--方法三   select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name;

  

原文地址:https://www.cnblogs.com/itplay/p/10983244.html