查询所有列
DQL(查询)
select * from emp;
或输入相关参量
select ename,job,mgr,hiredate,sal,comm,deptno,empno from emp;

-
投影查询
select ename,job,hiredate,sal,comm from emp; -- 投影查询

-
查询中的运算
--查询每个员工的姓名及年收入
select ename 员工姓名,sal * 12 年收入 from emp;

--查询每个员工的姓名及年(收入+奖金)
select ename 员工姓名,(sal + COALESCE(comm,0)) * 12 年收入 from emp;

-
条件查询
-- 查询工资大于1000的职员
--每次执行的时候,选中初始化为零的指令(use mydb;),不然数据会叠加。
use mysdb;
select * from emp where job='CLERK' and sal > 1000

-- 查询工资在1000-4000之间的职员或销售员的姓名及年收入
select ename 姓名,sal * 12 年收入 from emp where sal between 1000 and 4000 and (job = 'SALESMAN' or job = 'CLERK');
或
select ename 姓名,sal * 12 年收入 from emp where sal between 1000 and 4000 and job in ('SALESMAN','CLERK');
模糊查询
-- 查询姓S的员工信息 模糊查询
select * from emp where ename like 'S%';

按页、条查询
-- 假设每页数据有2条,我想查第二页员工数据
select * from emp limit 0,2;

-- 查询第1,3,6,9条员工数据
--在mysql数据库设置一个变量的语法
冒号等号:表示赋值
用括号括起来的叫做子查询视图,即将查询结果再作为导出视图,再在这个视图中进行查询。
set @rownum=0;
select * from (select @rownum:=@rownum+1 序号,ename,job from emp)t where t.序号 in (1,3,6,9);

--涉及子查询:先在子查询中创建一个带序号的视图(视图要有名t),在针对视图查询
-- 查询工资大于83年后入职的任意一个员工工资的员工数据
select * from emp where sal > any(select sal from emp where hiredate > str_to_date('1983-0-1','%Y-%m-%d'));

-- 查询81到85年的员工数据
select * from emp where hiredate between str_to_date('1981-0-0','%Y-%m-%d') and str_to_date('1985-0-0','%Y-%m-%d');

-
统计查询
--五个统计函数
--求最大值
select max(sal) from emp;
select CONCAT('最大值 ',max(sal),'人命币') from emp;

--求最小值
select CONCAT('最小值',min(sal),'人民币') from emp;

--求平均值
select CONCAT('平均值',avg(sal),'人命币') from emp;

--求和
select CONCAT('总共',sum(sal),'人命币') from emp;

--求总记录数
select CONCAT('总共',count(*),'条') from emp;


293

被折叠的 条评论
为什么被折叠?



