基本查询语句: * 表示所有的列名
select * from 表名;
select * from scott.emp;
选择部分列进行查看:
select 列名1,列名2… from 表名;
select empno,ename,sal from scott.emp;
选择部分行进行查看:
select 列名1,列名2… from 表名 where 列名=值;
select empno,ename,sal from scott.emp where sal>=3000;
a. 精确查询
where 列名=值
select * from scott.emp where deptno=10;
b. 范围筛选 > < >= <= !=
where 列名>=值
select * from scott.emp where sal<1000;
c. 模糊筛选
where 列名 like 值
select * from scott.emp where ename like ‘A%’;
A开头,第四个字母是E的名字
select * from scott.emp where ename like ‘A__E%’;
select * from scott.emp where ename like ‘__A%’;
% 通配符 匹配其他的任意长度的任意内容
_ 通配符 匹配一个任务的内容
查询名字里面包含了%的姓名
select * from scott.emp where ename like ‘%%%’ escape ‘’;
\ 是转义符的意思,转义符可以将特殊的符号,变成普通的字符;
但是要通过escape关键字告诉sql语句,这个时候\是当成转义符来使用的。
d. 逻辑筛选 and(两个条件要同时满足) or(两个条件满足一个就行) not
先运行and ,再运行 or
e. 空值的查询
where 列名 is null
f. 和范围相关的查询关键字 in between…and…
查询7369 7788 7654这三个员工的信息
select * from scott.emp where empno=7369 or empno=7788
or empno=7654;
select * from scott.emp where empno in(7369,7788,7654);
查询工资在1000到3000范围内的所有员工信息
select * from scott.emp where sal>=1000 and sal<=3000;
select * from scott.emp where sal between 1000 and 3000;
本文介绍了SQL中的基本查询操作,包括使用SELECT语句选择全部或部分列,WHERE子句进行精确、范围、模糊和逻辑筛选,以及对空值和特定范围的查询。通配符如%和_在模糊查询中的应用,以及IN和BETWEEN关键字在范围筛选中的使用也得到了阐述。
2087

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



