1.1  查询语句

1.1.1     select

select 用于从数据看查询数据。语法

select field1,filed2,.. .
from tablename
[where condition]
 -- 查询所有员工的名字和雇员号
select empno,ename from emp; -- 查询所有员工的雇员号、姓名、岗位
select empno,ename,job from emp; -- 字段的别名 as
select ename as "姓名" from emp;
select ename as "姓名",job as "岗位" from emp; -- 别名一定要用双引号,不能用单引号
select ename "姓名",job "岗位" from emp;
-- 双引号可以省略
select ename 姓名 from emp; -- 表的别名
select emp.ename,emp.job from emp;
select e.ename,e.job from emp e;

  *  是通配符表示查询所有字段。如果要查特定的字段时,不要使用*,影响查询效率。

1.1.2 distinct 去重

把重复性的记录去掉,只保留一条。

select empno,ename,job,mgr,hiredate,sal,comm,deptno
from emp;
-- * 通配符表示所有字段
select * from emp;

修饰多字段时,多个字段的值都不一样才保留。

1.1.3     where 子句

where 表示查询的条件。

[1] =,!= ,<>,<,>,<=,>= 关系运算符

<> 表示不等于

 -- where 子句

 -- 把部分10的雇员查询出来
select *
from emp
where deptno = 10; -- 把名称为smith的雇员
select e.*
from emp e
where e.ename = 'SMITH'; -- 查询底薪大于等于1000的员工
select e.*
from emp e
where e.sal >= 1000; select e.*
from emp e
where e.sal <> 800

any/som/all(list)

any/some(list) 满足list列表中的任意一个条件

all(list) 满足list列表的中所有条件

 -- any some all

 -- 查询薪资大于1000或者薪资大于800的雇员
select e.*
from emp e
where e.sal > some(1000,800); -- 查询薪资大于1000
select e.*
from emp e
where e.sal > all(1000,800);

[2] null

null 在sql中表示的是不确定 => 可以认为没有值(一些情况下)

 -- null/not null
-- 查询没有津贴的雇员
select e.*
from emp e
where e.comm is null select e.*
from emp e
where e.comm is not null

【3】betweem x and y

表示一个值位于【x,y】区间,x与Y一般都是数字

-- between x and y
-- 查询薪资在1000-5000之间的雇员
select e.*
from emp e
where e.sal between 1000 and 5000 -- 查询薪资在(3000,5000]之间的雇员
select e.*
from emp e
where e.sal between 3000.01 and 5000

【4】in/not in list

表示字段值是否在list列表中

 -- in/not in(list)
-- 查询部分号是10和20的员工
select e.*
from emp e
where e.deptno in(10,20); select e.*
from emp e
where e.deptno not in(10,20); -- 查询薪资是1000,2000,5000的员工
select e.*
from emp e
where e.sal in (1000,2000,5000);

【5】模糊查询

like 关键字 用于模糊查询中

%:表示任意字符出现多次(含0次),

_:表示任意字符出现1次。

escape(‘x’)表示指定的转义字符为x,一般指定为\

-- 查询名字是c开头的雇员

select e.*
from emp e
where e.ename like 'c%'; -- 查询名字中第二个字母是M的雇员
select e.*
from emp e
where e.ename like '_M%' -- 查询名字中含有M的雇员
select e.*
from emp e
where e.ename like '%M%'; -- 查询名字中含有%的雇员
select e.*
from emp e
where e.ename like '%\%%' escape('\');

1.2 复杂查询(and /or)

where 后面的条件可以跟多个and或者or连接

and:且、并且

or:或、或者

 -- 查询部门10且薪资大于等2000的雇员
select e.*
from emp e
where e.deptno = 10 and e.sal >= 2000; -- 查询名字中含M且薪资大于1000的雇员
select e.*
from emp e
where e.ename like '%M%' and e.sal > 1000 -- 查询部门在10或20的雇员
select e.*
from emp e
where e.deptno = 10 or e.deptno = 20

where 中and、or的执行效率问题

-- 思考:查询条件的顺序对查询速度是否有影响?

/*
分析:
and 表示且,条件越多,检索的数据量越来越少
or 表示或,条件越多,检索的数据量越来越多
where 条件的执行顺序从后向前
*/ -- 优化后的sql
select e.*
from emp e
where e.sal>=2000 and e.deptno = 10
-- 结论
-- AND: 把检索结果较少的条件放到后面 -- 查部门10或30的雇员
select e.*
from emp e
where e.deptno = 10 or e.deptno = 30;

综合案例

 --使用in查询部门名称为 SALES 和 RESEARCH 的雇员姓名、工资、部门编号
-- 思考:部门名称位于dept,雇员信息位于emp表
select e.ename,e.sal,e.deptno
from emp e
where e.deptno in
(
select d.deptno
from dept d
where d.dname = 'SALES' or d.dname = 'RESEARCH'
);

1.3  计算字段

我们经常需要把数据库中检索出来的信息进行再加工,允许的操作+、-、*、/。通过四个运算得到新的字段(计算字段)。

计算字段在数据表中不存在。

-- 查询出每个雇员的月薪(收入)
select e.ename,e.sal+e.comm as "收入",e.deptno
from emp e

注意:很多记录中的comm是null,表示不确定的值,经常四则运算后的值也不确定。

当遇到字段时null时,可以通过nvl函数把null转化便于运算的类型。

-- nvl函数优化
select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno
from emp e

Select 查询语句的更多相关文章

  1. create table 使用select查询语句创建表的方法分享

    转自:http://www.maomao365.com/?p=6642 摘要:下文讲述使用select查询语句建立新的数据表的方法分享 ---1 mysql create table `新数据表名` ...

  2. Mysql常用sql语句(3)- select 查询语句基础使用

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 针对数据表里面的每条记录,select查询语句叫 ...

  3. 转载《mysql 一》:mysql的select查询语句内在逻辑执行顺序

    原文:http://www.jellythink.com/archives/924 我的抱怨 我一个搞应用开发的,非要会数据库,这不是专门的数据库开发人员干的事么?话说,小公司也没有数 据库开发人员这 ...

  4. Mybatis-plus在原有的select查询语句中动态追加查询条件

    一.适用场景 1.使用了xml形式的mapper.2.不想在select查询中大量使用<if>标签来判断条件是否存在而加入条件. 二.步骤 1.自定义wrapper继承QueryWrapp ...

  5. MySQL命令:select查询语句

    SQL 中最常用的 SELECT 语句,用来在表中选取数据. 要记得的知识点如下: SELECT 语句格式: SELECT 要查询的列名 FROM 表名字 WHERE 限制条件: WHERE语句后: ...

  6. select查询语句执行顺序

    查询中用到的关键词主要包含六个,并且他们的顺序依次为select--from--where--group by--having--order by其中select和from是必须的,其他关键词是可选的 ...

  7. 一条SELECT查询语句在数据库里执行时都经历了什么

    每天都在跟 mysql 打交道,你知道执行一条简单的 select 语句,都经历了哪些过程吗? 首先,mysql 主要是由 server 层和存储层两部分构成的.server 层主要包括连接器.查询缓 ...

  8. Select查询语句2

    一.模糊查询 1.语法结构 select*from table_name where column like '%context%' 在使用like运算符时如果不使用通配符“%”,则like的作用与= ...

  9. Select查询语句1

    一.语法结构 select[all|distinct]select_list from table_name[join join_condition] where search_condition g ...

随机推荐

  1. python 路径引用问题

    文件结构 入口文件· 将当前文件的父级,加入搜索目录里面 import sys import os current_dir = os.path.abspath(os.path.dirname(__fi ...

  2. andriod studio命名规范

    标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外,其余所有单词的第一个字母大写. 2 帕斯卡(pascal)命名法:又称大驼峰命名法,所有单词的第一个字 ...

  3. Mysql插入多条数据测试

    --新建存储过程 create procedure doinsert3() begin declare i int; declare j int; set i = 0; set j = 0; whil ...

  4. 第06组 Alpha冲刺(1/6)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 宋奕 过去两天完成了哪些任务 主要完成了用户模块接口 具体完成了用户注册登录,忘记.修改密码,修改.查看个人信息 GitHu ...

  5. Spark(四十六):Spark 内存管理之—OFF_HEAP

    存储级别简介 Spark中RDD提供了多种存储级别,除去使用内存,磁盘等,还有一种是OFF_HEAP,称之为 使用JVM堆外内存 https://github.com/apache/spark/blo ...

  6. 英语语法 - 介词on/in/at与时间

    介词滥用是中国学生的普遍缺点,主要是在语言学习的阶段没有人指正,形成有效的反馈,后面进入社会,就算别人发现你错了,也不会指正你,导致你一错再错. 看一篇教程,讲得不错.

  7. 解决Wireshark安装Npcap组件失败

    解决Wireshark安装Npcap组件失败   从Wireshark 3.0开始,Npcap取代Winpcap组件,成为Wireshark默认的网卡核心驱动.由于该组件属于驱动程序,所以安装时候容易 ...

  8. Intellij IDEA 2019 最新优化配置

    Intellij IDEA 2019 最新优化配置     转发自Dimple’s Blog 摘要: 之前在CSDN上写了一点关于IDEA的优化配置之类的文章,有些图片失效了,很多人都希望会有继续更新 ...

  9. Android利用canvas画各种图形

    Android利用canvas画各种图形(点.直线.弧.圆.椭圆.文字.矩形.多边形.曲线.圆角矩形) 本文链接:https://blog.csdn.net/rhljiayou/article/det ...

  10. ISO/IEC 9899:2011 条款6.7.3——类型限定符

    6.7.3 类型限定符 语法 1.type-qualifier: const restrict volatile _Atomic 约束 2.除了指针类型(其被引用的类型是一个对象类型)之外的类型,不应 ...