MySql
1.Mysql常见命令
use qfdb1;
# ctrl +变大字体
# 显示数据库
show databases;
# 控制台运行mysql
# mysql -u root -p;
#创建数据库
create database qfdb3;
#修改数据库
alter database qfdb3 character set gbk;
#查看数据库的创建信息
show create database qfdb1;
#删除数据库
drop database qfdb1;
# 查看当前选择的数据库
select database();
#切换数据库
use qfdb1;
2.基本查询
2.1 语法
语法:select 列名 from 表名
2.2 使用
1.对查询到的列中的数据进行加减乘除±*/
2.%在mysql中是占位符,也可以作为模运算
3.查询结果去重 distinct
4.列起别名 as
#语法 select 列名 from 表名
#1.1 基本查询
# 查询所有类
select * from t_employees;
#1.2 部分列查询
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees;
#1.3 查询中执行运算
select 10+20;
select 10-5;
select 5%3;
select 10/4;
select SALARY *12 from t_employees;
select SALARY * COMMISSION_PCT from t_employees;
2.3 排序查询
1.对查询的结果进行排序:
语法:select 列名 from 表名 order by 排序列【排序规则】
asc 升序
desc 降序
use companyDB;
#1 基本查询
#1.1 *查询所有类
select * from t_employees;
#1.2 部分列查询
select employee_id,first_name,salary from t_employees;
#1.3 查询中执行运算
select 10+20;
select 10-5;
select 5%3;
select 10/4;
select salary*12 from t_employees;
select salary*commission_pct from t_employees;
#1.4 列起别名 as
select employee_id as 编号,first_name as 名字,salary as 工资 from t_employees;
select employee_id 编号,first_name 名字,salary 工资 from t_employees;
#1.5 查询结果去重 distinct
select distinct salary , commission_pct from t_employees;
#2 排序查询
#2.1查询员工信息 编号,名字,工资,按照工资的升序排列 asc 升序 desc 降序
select employee_id,first_name,salary from t_employees order by salary desc, employee_id asc;
#3 条件查询
#3.1 查询工资等于11000的员工信息
select employee_id,first_name,salary from t_employees where salary=11000;
#3.2 逻辑判断,查询工资等于11000的员工信息并且奖金等于0.30
select employee_id,first_name,salary, commission_pct from t_employees where salary=11000 and commission_pct=0.30;
#3.3 查询工资小于等于10000 并且大于等于6000的员工信息
select employee_id,first_name,salary from t_employees where salary >= 6000 and salary<=10000;
#3.4 查询工资小于等于10000 并且大于等于6000的员工信息 使用between and
select employee_id,first_name,salary from t_employees where salary between 10000 and 6000 ;
#3.5 查询奖金为null,NULL 值判断 not位置:放在is后面或者放在字段前面
select employee_id,first_name,salary,commission_pct from t_employees where commission_pct is not null;
#3.6 查询员工的部门id为 枚举查询( IN (值1,值2,值3 ) )
select employee_id,first_name,salary,department_id from t_employees where department_id=90 or department_id=60 or department_id=30;
#3.7 查询员工姓名以‘L’开头 模糊查询(LIKE 通配符)
# % 实现求余 实现模糊查询 表示任意多个字符
select employee_id,first_name,salary from t_employees where first_name like 'L%';
#3.8 查询员工姓名以‘L’开头 四个字母 模糊查询(LIKE 通配符)
# _ 表示一个字符
select employee_id,first_name,salary from t_employees where first_name like 'L___';
#4 分支结构查询
#4.1查询员工的工资,工资大于等于15000显示高,大于等于10000的中等,否则低
select employee_id,
case
when salary >=15000 then '高'
when salary >=10000 then '中等'
else '低'
end
from t_employees;
#5 MySQL中函数的使用
#5.1 日期函数
#当前系统时间
select SYSDATE();
select NOW();
#当前日期
select CURDATE();
#当前时间
select CURTIME();
#获取当前日期是一年中第几周
select WEEK('2021-4-6');
#获取年份
select YEAR('2021-4-6 10:20:30');
select MONTH('2021-4-6 10:20:30');
select DAY('2021-4-6 10:20:30');
select HOUR('2021-4-6 10:20:30');
select MINUTE('2021-4-6 10:20:30');
select SECOND('2021-4-6 10:20:30');
#获取两个日期的相差天数
select DATEDIFF('2021-4-6','2021-3-6');
#增加天数
select ADDDATE('2021-4-6',200);
#5.2 字符串函数
#合并 CONCAT(str1,str2,str....)
select CONCAT('hello','world');
select employee_id, first_name,salary from t_employees;
select CONCAT('编号是',employee_id, '的名字为',first_name,'工资是',salary) as info from t_employees;
#INSERT(str,pos,len,newStr) 插入替换
select insert('java是世界上最好的语言',6,2,'地球');
#截取SUBSTRING(str,pos,len)
SELECT SUBSTRING('java是世界上最好的语言',1,4);
SELECT SUBSTR('java是世界上最好的语言',1,4);
#获取字符串的字节长度
select LENGTH('java是世界上最好的语言');
#获取字符串字符长度
select CHAR_LENGTH('java是世界上最好的语言');
# 5.3聚合函数
#SUM求和
select sum(salary) from t_employees;
#AVG平均
select avg(salary) from t_employees;
#MAX最大值
select max(salary) from t_employees;
#MIN最小值
select min(salary) from t_employees;
#COUNT数据个数,如果有NULL,忽略
select count(*) from t_employees;
#其他函数
#DATABASE();
select database();
#IFNULL 如果为NULL,返回0
select salary+salary*commission_pct from t_employees;
select salary+salary*IFNULL(commission_pct,0) from t_employees;
#USER();返回当前用户
select USER();
#VERSION():返回版本
select VERSION();
#6分组查询
#6.1 查询每个部门的员工人数
#注意 SELECT 后面查询列 必须是分组列或聚合列
select department_id,count(*),avg(salary) from t_employees GROUP BY department_id;
#6.2 查询每个部门的员工平均工资,并且平均大于等于10000,分组过滤查询 HAVING
select department_id,avg(salary) from t_employees GROUP BY department_id having avg(salary)>=10000;
#7 限制查询
#7.1查询前三名的学生信息
#select * from scores order by result;
#7.2实现分页查询
#107条数据,每页10条,一共11页
#查询第一页
SELECT * from t_employees order by employee_id limit 0,10;
#查询第二页
SELECT * from t_employees order by employee_id limit 10,10;
#查询第三页
SELECT * from t_employees order by employee_id limit 20,10;
#最后一页
SELECT * from t_employees order by employee_id limit 100,10;