MySQL -- DQL

1、select查询列和列名:
--查询所有员工信息(*通配符,默认查询所有的列)
select * from emp;

--查询员工的姓名
select ename from emp;

--查询员工的薪资
select sal from emp;

--查询员工的姓名和薪资
select ename , sal from emp;
select ename sal from emp;
select ename sal comm from emp;

--查询员工的姓名和薪资,推荐使用单引号
select ename '员工姓名', sal "薪资" from emp;

--查询到的数据可以直接进行运算
select ename ,sal ,sal * 12 from emp;
select ename ,sal ,comm ,(sal+comm) * 12 from emp;
2、select的条件查询,普通条件查询 => < != <> >= <=
--查询员工编号为7369的员工
select ename,sal from emp where empno = 7369;

--查询员工姓名叫做SMITH
select ename,deptno,job from emp where ename = 'SMITH';
select ename,deptno,job from emp where ename = 'smith';

--查询薪资大于2000的员工姓名
select ename from emp where sal > 2000;

--查询工作为SALESMAN
select * from emp where job = 'SALESMAN';

--查询部门在20的员工
select * from emp where deptno = 20;

--查询薪资不等于2000的员工
select * from emp where sal != 2000;
select * from emp where sal != 2000;
3、in 在某个范围中查找
--查询 员工编号为 7369 7788 7881的员工信息
select * from emp where empno in (7369,7788,7881);

--查询 员工编号除了 7369 7788 7881之外的所有员工信息
select * from emp where empno not in(7369,7788,7881);

--查询除了10,20部门之外的所有员工
select * from emp where deptno not in (10,20);
4、null值查询
--查询不发放津贴的员工信息
select * from emp where comm is null;

--查询发放津贴的员工信息
select * from emp where comm is not null;
5、范围比较
--查询薪资范围在1000-4000之间的员工信息 [1000.4000]
select * from emp where sal between 1000 and 4000;
6、模糊查询 % _
--查询名字中有S的员工
select * from emp where ename like '%S%';

--查询名字最后一个字符是S
select * from emp where ename like '%S';

--查询名字第一个字符是S
select * from emp where ename like 'S%';

--查询名字第二个字符是A
select * from emp where ename like '_A%';

--查询名字中有%的员工
select * from emp where ename like '%\%%';
--查询名字第8 188个字符是A,这是需要一些特殊的手段-》函数
-- % 代表任意字符的任意次数 _任意字符的一次
7、 多条件联合查询 and or
--and 必须前后同时满足条件才能返回结果
--or前后有一个满足条件就能返回结果

--查询在20部门并且薪资大于2000的员工
select * from emp where deptno =20 and sal >2000;

--查询在20部门或者薪资大于2000的员工
select * from emp where deptno = 20 or sal >2000;

--查询不在20部门并且薪资小于2000的员工
select * from emp where deptno <> 20 and sal <2000;
8、select结果排序 order by,使用asc是升序排列(默认),使用desc可以降序排序
        1、单列
--按照薪资进行排序(默认升序)
select * from emp order by sal;

--按照薪资进行排序(降序)
select * from emp order by sal desc;

--按照薪资进行排序(升序)
select * from emp order by sal asc;

--按照津贴进行排序(null排在最前面)
select * from emp order by comm;
        2、多列
--多个排序的列
select * from emp order by deptno,sal;

--多个排序的列(部门升序 薪资降序)
select * from emp order by deptno,sal desc;

--多个排序的列(工作,薪资)
select * from emp order by job,sal;
9、select结果分页
--每次查询前N行
SELECT
 * 
FROM
 emp 
 LIMIT 4;
 
--查询第N页,每页显示M个
select * from emp limit 0,3;
select * from emp limit 3,3;
select * from emp limit 6,3;
select * from emp limit (n-1)*M,M;

--查询薪资大于1000的逆序排列,然后显示前5条记录
select * from emp where sal >1000 order by sal desc limit 0,5 ;
1、字符串函数:



1、计算字符串的长度:length
select  name ,length(name) from emp;


2、截取字符串的长度:截取的字段,下表是从1开始的。

select   name ,substr(name,1,2)  from emp;


3、大小写的转化:

select   name ,upper(name),lower(name) from emp;


4、字符串做拼接:

select  comcat('word','hello');

5、字符串做替换:

select   replace (tom,'t',' ') from emp;  结果:  t m
日期函数:

1、获取当前的时间:
  curdate、current_date



select  curdate
        ,current_date
        ,current_time;


2、日期格式的转换:

 - select DATE_FORMAT(sysdate(),'%Y-%m-%d %H:%i:%s')
 - select hiredate, date_format(now(),'%Y年%m月%d日 %H时%i分%s秒') from emp;



3、分别获取年、月、日、时、分、秒

select    year(curdate)
        ,month(curdate)
        ,day(curdate)
        ,hour(curdate)
        ,minute(curdate)
        ,second(curdate);


4、做日期的加减:

select  adddate(curdate,10);




5、将时间转成时间戳:
unix_timestamp (curdate);



6、两个日期相减的函数:

datediff(date1,date2):两个日期相减,date1减去date2得到相减之后的天数
    
   
数值函数:
-- 向上取整 向下取整
 - select ceil(12.1),floor(12.9) 
 
-- mod abs pow PI rand round TRUNCATE(直接进行截取,不进行四舍五入)
-- 保留多少位有效数字
 - select round(1.4999999,2),round(1.4999999),round(1.4999999,-1)
 - select TRUNCATE(1.4999999,2)
转换函数:
-- 日期--》字符串
 - date_format(date,expr)
 - select DATE_FORMAT(sysdate(),'%Y-%m-%d %H:%i:%s');
 
-- 字符串--》日期
 - 要注意字符串和格式的匹配
 - select STR_TO_DATE('2020-4-16 17:15:24','%Y-%c-%d %H:%i:%s');
 - select STR_TO_DATE('5月2022年4日','%m月%Y年%d日');
-- 空值的处理
	if null(exp1,exp2) exp1!=null?exp1:exp2
	select IFNULL(comm,888) from emp;
	
-- 加密算法
	select MD5('123456');
	select AES_ENCRYPT('123456','abcd'),AES_DECRYPT(AES_ENCRYPT('123456','abcd'),'abcd');
case when 函数的使用:

1、简单的使用:

case  sex  when '男'  then true else false end;


2、例如基本的使用:

用例:
 将不同的分数段进行分类:0-60 不及格、60-70 中等、70-80 良好 80-90 优秀

select 
      name, 
     ( case when score < 60 then '不及格'
     when score >60 and score <70 then '中等'
     when score >70 and score <80 then '良好'
     else '优秀' end
) as rank;
from 
score

在M有SQL中的执行的顺序是:

from  -- where -- group by -- select  --  having -- order by 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值