ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL学习记录

2022-04-15 18:01:16  阅读:130  来源: 互联网

标签:记录 age 表名 员工 学习 emp MySQL where select


sql分类,主要分为DDL,DML,DQL, DCL

DDL:定义数据库对象(数据库,表,字段)

DML:对数据库表中的数据增删改

DQL:数据查询语言,查询表中的记录

DCL:用来创建数据库用户,控制数据库的访问权限

DDL:

1.查看数据库:

show databases;

2.查看当前数据库:

select database();

3.创建数据库:

create database 数据库名;

create database if not exists 数据库名;

create database default charset utf8mb4;

4.删除数据库:

drop database if exists 数据库名;

5.切换数据库;

use 数据库名;

表操作:

1.查看当前数据库所有表

show tables;

2.查看表结构:

desc 表名;  

3.查看建表语句;

show create  table 表名;

4.创建表结构

create table 表名(

字段1 字段类型 

)

create table user(

id int comment '主键ID',

name varchar(50) comment '姓名',

age int comment '年龄'

)comment '用户表';

表操作-修改

1.添加字段

alter  table 表名 add  字段名 类型(长度);

2.修改数据类型

alter table 表名 modify 字段名 新数据类型(长度);

3.修改字段名和字段类型

alter table 表名 change 旧字段名 新字段名 类型(长度) ;

4.删除字段

alter table 表名 drop 字段名;

5.修改表名

alter table 表名 rename to 新表名;

6.删除表

drop table if exists 表名;

7.删除指定表,并重新创建表

truncate table 表名;

DML:

1.添加数据

insert into 表名 (字段1,字段2,...)values(值1,值2,...);

2.给全部字段添加数据

insert into 表名 values (值1 ,值2 ,...);

3.批量添加数据

insert into 表名 (字段1,字段2,...)values (值1,值2,...),(值1,值2,...),(值1,值2,...);

insert into 表名 values (值1 ,值2 ,...),(值1 ,值2 ,...),(值1 ,值2 ,...);

4.修改数据

update 表名 set 字段名1 = 值1 ,字段名2 = 值2[where 条件];

5.删除数据

delete from 表名 【where 条件】;

DQL;

select

字段列表

from

表名列表

where

条件列表

group by

分组字段列表

having

分组后条件列表

order by 

排序字段列表

limit 分页参数;

基础查询(不带任何条件)

条件查询(where)

聚合函数(count,max,min, avg, sum)

分组查询(group by)

排序查询(order by)

分页查询(limit)

去重复

select distinct * from 表名;

比较运算符

>,>=,<,<=,=,<>或!=,between .. and ..,in(...),like 占位符,is null.

逻辑运算符

and 或&&,or或||,not 或!

查询年龄等于 88 的员工

 select * from emp where age =88;

查询年龄小于 20 的员工信息

 select * from emp where age <20;

查询年龄小于等于 20 的员工信息

 select * from emp where age <=20;

查询没有身份证号的员工信息 select * from emp where idcard is null; 查询有身份证号的员工信息 select * from emp where idcard is not null; 查询年龄不等于 88 的员工信息

 select * from emp where age <> 88;

select * from emp where age !=88;

查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息

select * from emp where age between 15 and 20;

select * from emp where age >=15 and age <=20;

select * from emp where age >=15 && age <=20;

查询性别为 女 且年龄小于 25岁的员工信息

 select * from emp where gender ='女' and age <25;

查询年龄等于18 或 20 或 40 的员工信息

 select * from emp where age =18 or age =20 or age =40;

select * from emp where age in(18,20,40);

查询姓名为两个字的员工信息 _ %

 select * from emp where name like '__';

查询身份证号最后一位是X的员工信息

 select * from emp where idcard like '%x';

NULL值是不参与所有聚合函数运算的。 统计该企业员工数量

 select count(*) from emp; -- 统计的是总记录数

 select count(idcard) from emp; -- 统计的是idcard字段不为null的记录数

统计该企业员工的平均年龄

 select avg(age) from emp;

统计该企业员工的最大年龄

 select max(age) from emp; 

统计该企业员工的最小年龄

select min(age) from emp;

统计西安地区员工的年龄之和

select sum(age) from emp where address ='西安'; 

SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组 后过滤条件 ]; where与having区别:

执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组 之后对结果进行过滤。
判断条件不同:where不能对聚合函数进行判断,而having可以。

根据性别分组 , 统计男性员工 和 女性员工的数量

 select gender count(*) from emp group by gender;

根据性别分组 , 统计男性员工 和 女性员工的平均年龄

 select gender,avg(age) from emp group by gender;

查询年龄小于45的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址

 select workaddress count(*) address_count from emp where age < 45 group by workaddress having address_count>=3;

统计各个工作地址上班的男性及女性员工的数量

 select workaddress,gender,count(*) 数量 from emp group by gender, workaddress; 

根据年龄对公司的员工进行升序排序

 select * from emp order by age;

根据入职时间, 对员工进行降序排序

 select * from emp order by entrydate desc;

根据年龄对公司的员工进行升序排序 , 年龄相同 , 再按照入职时间进行降序排序

 select * from emp order by age ,entrydate desc;

查询第1页员工数据, 每页展示10条记录

 select * from emp limit 0,10;

select * from emp limit 10; 

查询第2页员工数据, 每页展示10条记录

 select * from emp limit 10,10;

查询年龄为20,21,22,23岁的员工信息。

 select * from emp where age in (20,21,22,23);

查询性别为 男 ,并且年龄在 20-40 岁(含)以内的姓名为三个字的员工。

 select * from emp where gender='男' and (age between 20 and 40) and name like '___';

统计员工表中, 年龄小于60岁的 , 男性员工和女性员工的人数。

 select gender,count(*) 人数 from emp where age<60 group by gender;

查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按 入职时间降序排序。

 select name,age from emp where age <= 35 order by age,entrydate desc;

查询性别为男,且年龄在20-40 岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序。 select * from emp where gender='男' and (age between 20 and 40) order by age ,entrydate  limit 5;  执行顺讯  

 

DQL语句的执行顺序为: from ... where ... group by ... having ... select ... order by ... limit ... 

函数 

分为以下四类: 字符串函数、数值函数、日期函数、流程函数。 

1.concat : 字符串拼接 

select concat('hello','world');

2.lower : 全部转小写 

select lower('Hello');

3.upper : 全部转大写 

select upper('hello'); 

4.lpad : 左填充 

select lpad('1',5,'0');-- 向左填充0,直到为5位数。

5.rpad:右填充

select rpad('1', 5, '-'); 

6.trim : 去除空格 

select trim('hello world my name is zbc');

7.substring : 截取子字符串 

select substring('qwerty',1,3);

数值函数 

ceil :向上取整 

select ceil(1.1); 

floor:向下取整 

select floor(1.9); 

mod:取模 

select mod(7,4); 

rand:获取随机数 

select rand(); 

round:四舍五入

select round(2.344,2); 

日期函数 

curdate:当前日期 

select curdate();

curtime:当前时间 

select curtime();

 now:当前日期和时间 

select now(); 

select year(now());

select month(now());

select day(now());

date_add:增加指定的时间间隔 

select date_add(now(), INTERVAL 70 YEAR ); 

datediff:获取两个日期相差的天数

select datediff('2021-10-01', '2021-12-01'); 

查询所有员工的入职天数,并根据入职天数倒序排序。

select datediff(curdate(),entrydate) days from emp order by days desc;

流程函数 

 

 

标签:记录,age,表名,员工,学习,emp,MySQL,where,select
来源: https://www.cnblogs.com/gstszbc/p/16150407.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有