SQL简单查询、条件查询
select语法
select 列名,列名2,列名3…… from 表名;
语法:
where 跟在 from 之后--
select first_name||last_name ,salary,title 、、、 各种需要显示的字段列名
from s_emp 从哪个表
where salary > 1200; 条件是--------
order by 列名 desc/asc 降序升序写最后
其中注意事项
'*'代表所有信息
distinct 去除重复 栗子
从s_emp表中查询公司中所有职位
select distinct title from s_emp;
desc 描述命令,只能在命令窗口 ,查看表结构
select * from 表名 常看表的全部信息
--:表示注释
字符串拼接: || ‘’ || ,单引号内表示加内容,比如空格,点之类
栗子:select first_name||'`'||last_name||'是'||start_date||'入职的,工资是'||salary||'职位是'||title 备注 froms_emp
字符串运算:列名后直接加*+—% 栗子
--从员工表中查询每个员工姓名和年薪
select salary*12 from s_emp;
空值置换函数nvl() //nvl(列名,要转化的数值) ,,null值和所有其他数据计算,结果都是null
select
first_name,salary*12*(1+nvl(commission_pct/100,0)) 年薪
from
s_emp;
注意:nvl()中的两个值要为同一数据类型
比较运算符
>,<,=,<=,>=, != (^= ,<>) 不等于 字段后直接使用
逻辑运算符
and,or
where dept_id = 41 and salary > 1200;
in() 取多个数值,多个值使用逗号隔开
例:deot_id in(41,42,50) 相当于dept_id=41 or dept_id=42 or dept_id=50;
not in( , ,) 查询不在这里里面的
范围查询
between 2 and 6 在指定的范围之内,是全闭空间;相当于>=2 and <=6
dept_id>2 and dept_id<6 非全闭空间
not between 不在范围内
是否为空
is null
is not null // null 不能用 = 栗子:salary is null 而不是 salary = null
模糊查询:like ‘ ’ 注意用引号
not like (排除)
通配符:
_通配任意单个字符
%通配任意多个字符
栗子: from s_emp where first_name like '%a%';
排序字句:
select 列名1,列名2...
from 表名
where 查询条件
order by 列名 asc(升序,默认可以不写)| desc(降序)
多个列排序 order by sal asc ,name desc;

浙公网安备 33010602011771号