MySQL基础之DQL语言(一)

1.数据库的好处

1、持久化数据到本地
2、可以实现结构化查询,方便管理

2.SQL的优点和分类在这里插入图片描述

DQL(Data Query Language):数据查询语言
select
DML(Data Manipulate Language):数据操作语言
insert 、update、delete
DDL(Data Define Languge):数据定义语言
create、drop、alter
TCL(Transaction Control Language):事务控制语言
commit、rollback

3.数据库相关概念

1、DB:数据库,保存一组有组织的数据的容器
2、DBMS:数据库管理系统,又称为数据库软件(产品),用于管理DB中的数据
3、SQL:结构化查询语言,用于和DBMS通信的语言

4.MySQL服务的启动和停止

方式一:计算机——右击管理——服务
方式二:通过管理员身份运行
net start 服务名(启动服务)
net stop 服务名(停止服务)

5.MySQL服务的登录和退出

方式一:通过mysql自带的客户端
只限于root用户
方式二:通过windows自带的客户端
登录:
mysql 【-h主机名 -P端口号 】-u用户名 -p密码
退出:
exit或ctrl+C

6.MySQL的常见命令

1.查看当前所有的数据库
show databases;
2.打开指定的库
use 库名
3.查看当前库的所有表
show tables;
4.查看其它库的所有表
show tables from 库名;
5.创建表
create table 表名(

	列名 列类型,
	列名 列类型,
	。。。
);
6.查看表结构
desc 表名;

7.DQL查询语言的学习

1、基础查询:

#查询字段
select last_name,salary,email from employees;
#查询常量值
select 'john';
#查询函数
select version();
#查询表达式
select 100%98
#起别名:便于理解、区分查询字段名有重名的情况
select last_name as,first_name asfrom employees;
select last_name  姓,first_name  名 from employees;
#去重
select DISTINCT department_id from employees;
#拼接CONCAT
SELECT CONCAT(last_name,first_name) as 姓名 from employees;
#查询有空值的情况IFNULL
select CONCAT(last_name,',',first_name,',',IFNULL(commission_pct,0)) as out_put from employees;

2、条件查询:

分类:
一、条件表达式
	示例:salary>10000
	条件运算符:
	> < >= <= = != <>
	
select * from employees where salary>12000;

SELECT last_name,department_id from employees where department_id<>90;

二、逻辑表达式
示例:salary>10000 && salary<20000
逻辑运算符:

	and&&):两个条件如果同时成立,结果为true,否则为false
	or(||):两个条件只要有一个成立,结果为true,否则为false
	not(!):如果条件成立,则not后为false,否则为true
	
select last_name,salary,commission_pct FROM employees where  salary>=10000 and salary<=20000;

三、模糊查询
示例:last_name like 'a%'
#like:一般和通配符一起使用,%代表任意多个字符,_代表任意单个字符
select * from employees where last_name like '%a%';
select last_name,salary from employees where last_name like '__n_l%';
#查询第二个字符为_的姓,\为转义字符
select last_name from employees where last_name like '_\_%';

#BETWEEN and :包含临界值
select * from employees where employee_id BETWEEN 100 and 200;

#in:
SELECT last_name,job_id from employees where job_id in('IT_PROT','AD_VP','AD_PRES');

#IS NULL
SELECT last_name,commission_pct from employees where commission_pct is null;
SELECT last_name,commission_pct from employees where commission_pct is not null;

#安全等于:<=>,既可以判断null值,又可以判断普通值
SELECT last_name,commission_pct from employees where commission_pct <=> null;

在这里插入图片描述
在这里插入图片描述
3、排序查询

#降序排列
select * from employees order by salary desc;
#升序:默认升序
select * from employees order by salary asc;

SELECT * from employees WHERE department_id>=90 ORDER BY hiredate asc;

#按年薪排序
select *,salary*12*(1+IFNULL(commission_pct,0)) as 年薪 from employees ORDER BY salary*12*(1+IFNULL(commission_pct,0)) desc;

select * from employees ORDER BY salary asc,employee_id desc;

4、常见函数

单行函数:
一、字符函数
1.length:获取参数值的字节个数
select LENGTH('john');
2.concat拼接字符串
select concat(last_name,'_',first_name) 姓名 from employees;
3.upper、lower
select upper('john');
4.截取字符:SUBSTR、substring
select SUBSTR('李莫愁哈哈哈陆湛远',6) as out_put;
select SUBSTR('李莫愁哈哈哈陆湛远',1,3) as out_put;
#首字母大写,其余小写
select concat(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(SUBSTR(last_name,2))) out_put from employees;
5.instr:返回字串第一次出现的索引
select instr('哈哈哈123哈哈哈','123') as out_put from employees;
6.trim:去掉前后指定数据
select LENGTH(trim('    张翠山     ')) as out_put;
#去掉前后a:张翠aaaa山
select LENGTH(trim('a' from 'aaaa张翠aaaa山aaaaa')) as out_put;
7.lpad:用指定的字符*实现左填充指定长度10********小明
rpad:右填充
select lpad('小明',10,'*') as out_put;
9.replace:替换
select replace('aaabbb13','a','m') as out_put;
二、数学函数
1.round 四舍五入
select round(-1.55);
select round(-1.55,1);
2.ceil 向上取整
select ceil(1.52);
3.floor 向上取整
select floor(1.52);
4.truncate 截断1.6
select TRUNCATE(1.65666,1);
5.mod 取余
select mod(-10,-3);
三、日期函数
1.now 显示当前日期+时间
select now();
2.curdate 返回当前系统日期,不包含时间
select curdate();
3.curtime 返回当前时间,不包含日期
select curtime();
4.获取指定部分:年、月、日、时...
select year(now());
5.STR_TO_DATE 将字符通过指定的格式转换成日期
select STR_TO_DATE('1998-2-3','%Y-%c-%d') as out_put;
select * from employees where hiredate=STR_TO_DATE('4-3 1992','%c-%d %Y');
6.date_format 将日期转换成字符
select date_format(now(),'%y年%m月%d日') as out_put;
select last_name,DATE_FORMAT(hiredate,'%m月/%d日 %y年') 入职日期 from employees where commission_pct is not null;
四、其他函数
	version版本
	database当前库
	user当前连接用户
五、流程控制函数
1.if函数
select if(10>5,'大','小');
2.case函数使用一:
select salary as 原始工资,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资 from employees;
3.case函数使用二:
select salary,
case 
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
end as 工资级别 from employees;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值