本文共 2373 字,大约阅读时间需要 7 分钟。
木木人
分享自身的一些所见所得
公众号
1create database ruozedata;
1grant all privileges on ruozedata.* to ruoze@'%' identified by '123456';2flush privileges;
1查看数据库:2show create database ruozedata;3查表结构:4show create table stuinfo;5查看用户权限:6show grants for ruoze@'%';
11.字段类型 2数值类型 3int 整数 4long 长整数 5 6float 单精度 7double 双精度 8decimal 小数值 钱挂钩的 910char 定长字符串 0-255字节 11varchar 变长字符串 0-65535字节 12text 1314日期和时间15date YYYY-MM-DD 2019-09-1016time HH:MM:SS 10:10:1017datetime 2019-09-10 10:10:1018timestamp 2019-09-10 10:10:10
1创建表: 2create table 表名( 3字段名 类型,…… 4) 5示例: 6create table stuinfo( 7id int auto_increment primary key, 8num int, 9name varchar(100),10age int,11createtime timestamp default current_timestamp,12createuser varchar(100),13updatetime timestamp default current_timestamp on update current_timestamp,14updateuser varchar(100)15)16添加字段:17alter table 表名 add 字段名 字段类型18alter table stuinfo add name varchar(100);
1insert into 表名(字段名1,字段名2,……)2values(值1,值2,……)3示例:4insert into ruozedata.stuinfo(num,name,age) 5values(1,'ruoze',12,……);
1update 表名 set 字段名=新值 2示例:3update stuinfo set age=15 where id=1;
1select * from 表名:2示例:3select * from stuinfo 普通查询4select * from stuinfo 带条件查询5where id >1;6select * from stuinfo 多条件查询7where id >2 or name='jepson';
1delete from 表名2示例:3delete from stuinfo where id=3;
1group by出现的字段 务必出现在 select 后面 2having 过滤 等价于 子表+where 3按照部门分组,查询各部门的薪水和跟部门人数 4select 5deptno ,sum(sal) as saal, 6count(ename) 7from emp 8group by deptno; 9找薪水和>9000的是哪个部门?10select11deptno,sum(sal) as saal12from emp13group by deptno14having saal >9000;
1通过部门,薪水排序2select 3*4from emp5order by deptno,sal desc;
1# left join 以左表为主 a<--b a数据最全 b是匹配 匹配多少算多少 on就是匹配条件 2select 3a.*, 4b.* 5from testa as a 6left join testb as b on a.aid=b.bid 7 8#right join 以右表为主 a-->b b数据最全 a是匹配 匹配多少算多少 on就是匹配条件 9select 10a.*,11b.*12from testa as a 13right join testb as b on a.aid=b.bid1415#inner join 16select 17a.*,18b.*19from testa as a 20inner join testb as b on a.aid=b.bid;
1#哪些部门的哪些职业的薪水和,最高1位的职业是什么? 2create view sal 3as 4select 5deptno,job, 6sum(sal+ifnull(comm,0)) as sal 7from emp 8group by deptno,job; 910select * from sal;1112select 13a.*14from sal a 15where16(17select count(*) from sal b 18where a.deptno=b.deptno19and a.sal
转载地址:http://lzqof.baihongyu.com/