设为首页 加入收藏

TOP

sql简单查询,限定查询及排序
2015-11-21 02:04:08 】 浏览:2566
Tags:sql 简单 查询 限定 排序

SQL(structured Query Language 结构化查询语言)是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统

SQL语言包含4个部分:

数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。

数据操作语言(DML),例如:INSERT、UPDATE、DELETE语句。

数据查询语言(DQL),例如:SELECT语句。

数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。


简单查询

简单的SQL查询语句的语法格式是:

Select *|字段列表别名

From 表名称;


-- 查询全部列
select * from emp;

-- 查询指定列
select empno, ename, job from emp;

-- 指定列别名
select empno 编号, ename 姓名, job 工作 from emp;
select empno as 编号, ename as 姓名, job as 工作 from emp;

-- 去重复结果
select job from emp;
select distinct job from emp;

-- 连接列结果
select '编号是' || empno || '雇员的' || '姓名是:'|| ename || ', 工作是:'|| job from emp;

-- 简单的四则运算
select sal*12 年薪 from emp;

限定查询

限定查询的语法格式为

Select{distinct}*|具体的列名 别名

From 表名称

{Where 条件语句};

-- 查询出工资大于1500 的所有员工
select * from emp where sal > 1500;

-- 查询每月可以得到奖金的雇员信息
select * from emp where comm is not null; 

-- 查询没有奖金的职工信息
select * from emp where comm is null;

-- 查询出基本工资大于1500,并且可以领取奖金的职工信息
select * from emp where sal > 1500 and comm is not null;

-- 查询出基本工资不大于1500,同时不可以领取奖金的职工的信息
select * from emp where sal <= 1500 and comm is null;

-- not 可以对条件取反,把条件真变为假,把假变为真 
select * from emp where not(sal > 1500 or comm is not null);

-- 查询基本工资大于等于1500,但是小于等于3000的职工的全部信息
select * from emp where sal >= 1500 and sal <= 3000;

-- between 关键字,sql中提供了一个专门的指定范围的过滤语句 BETWEEN … AND … 格式为:BETWEEN 最小值 AND 最大值  
select * from emp where sal between 1500 and 3000;

-- 查询出生在1981年1月1日到12月31日的雇员信息
select * from emp where hiredate between date '1981-01-01' and date '1981-12-31';
select * from emp where hiredate between '1-1月-81' and '31-12月-81';
select * from emp where hiredate between '1-1月-1981' and '31-12月-1981';

-- 查询出姓名是SMITH 的雇员信息
select * from emp where ename = 'SMITH';

-- 查询出雇员编号是 7369,7499,7521的雇员的信息
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;

-- in 关键字 in(val1, val2, val3 ... valn)
select * from emp where empno in (7369, 7499, 7521);

-- 查询出雇员编号不是7369,7499,7521的雇员的信息
select * from emp where empno not in(7369, 7499, 7521);

-- 查询出姓名为 SMITH,ALLEN,KING的雇员信息
select * from emp where ename in ('SMITH','ALLEN', 'KING');

-- 查询出雇员第二个字母为“L”的雇员信息
select * from emp where ename like '_L%';

-- 查询出雇员姓名以字母“S”开头的雇员信息
select * from emp where ename like 'S%';

-- 查询出雇员姓名包含字母“S”的雇员信息
select * from emp where ename like '%S%';

-- 查询入职年份为81年的雇员信息
select * from emp where hiredate like '%81';

-- 查询工资值中包含数字5的雇员信息
select * from emp where sal like '%5%';

-- 查询所有部门号不是10的雇员信息
select * from emp where deptno <> 10;
select * from emp where deptno != 10;

排序

排序的语法格式为

Select {distinct} *|具体的列 别名

From 表名称

{Where 条件}

{Order By 排序字段1,排序字段2 ASC|DESC}

-- 按工资升序对雇员信息进行排序
select * from emp order by sal asc;

-- 按工资降序对雇员信息进行排序
select * from emp order by sal desc; 

-- 检索出部门号为30的雇员信息,并按照工资降序,工资相同则按照入职日期升序排列
select * from emp where deptno = 30 order by sal desc, hiredate asc;
select * from emp where deptno = 30 order by sal desc, hiredate;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where deptno = 30 order by 6 desc, 5 asc;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where deptno = 30 order by 6 desc, 5;
select * from emp where deptno = 30 order by 6 desc,5;


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SQL-UsingCrossJoins 下一篇NavicatPremium怎样打开SQL文件.M..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目