where 子句-----限制行的返回
1.符合where后条件的数据返回,不符合where后条件的就过滤掉
查询工资大于1200的first_name,salary
select first_name,salary
from s_emp
where salary>1200;
2.字符串和数字的对比
查询工资等于2500的人的first_name
select first_name,salary
from s_emp
where salary=2500;
查询first_name是Carmen的工资
select first_name,salary
from s_emp
where first_name='Carmen';
select first_name,salary
from s_emp
where first_name='carmen';
//no rows selected
注意:字符串的值是区分大小写的,字符串是大小写敏感。例如:'a' 'A' 是不同的。
3.sql中的运算(比较)符号
<1>.逻辑比较运算符号
= < > >= <=
<2>.sql的比较运算符号
between 低值 and 高值
表达一个闭区间[1500,2500],找出工资在[1500,2500]之间的first_name,salary
select first_name,salary
from s_emp
where salary
between 1500 and 2500;
4. in (列表)
列表 -----用逗号隔开的一组值
找出id 是1或者是3或者是9的first_name,
salary
select first_name,salary
from s_emp
where id in(1,3,9);
in:列表中的顺序和数据组成相关
把出现概率高的放前面
5.is null ----判断一个值是不是NULL
找出提成是NULL的first_name,salary
select first_name,salary from
s_emp where commission_pct is null;
6.模糊查询
like '字符串'
龙 成龙 李小龙 龙孩儿 龙龙
找出所有的first_name 带a的,like '通配字符串'
sql的通配符
% 代表任意字符 0-n
_ 代表一个任意字符 1(能且只能是一个)
select first_name from s_emp where
first_name like '%a%';
找出第二个字符是a的first_name
select first_name from s_emp where
first_name like '_a%';
1.数据字典表:
user_tables
table_name 表名
s_emp s_dept
select table_name from user_tables;
把s_开头的表名列出来
select table_name from user_tables where table_name like 's_%';
2. 数据字段表把所有数据的值 默认处理成大写
select table_name from user_tables where table_name like 'S_%';
通配符的转义:
select table_name from user_tables where table_name like 'S\_%' escape '\';
3.条件连接符号
c 和c++
int a=10;
if(5<a<9){
printf("shenma shidao");
}
与 and
或 or
非 not
找出工资在[1500,2500]之间的
first_name,salary
select first_name,salary from s_emp
where salary>=1500 and salary<=2500;
找出工资在(1500,2500)之间的
first_name,salary
select first_name,salary from s_emp
where salary>1500 and salary<2500;
找出id 是1或者是3或者是9的first_name,
salary
select first_name,salary from s_emp
where id=1 or id=3 or id=9;
> <=
< >=
= != ^= <>
between and not between and
in not in
like not like
is null is not null
找出提成不是NULL的first_name,salary
select first_name,salary from s_emp
where manager_id is not null;
4.条件的优先级问题
那个sql语句查出的结果可能会多
order by 子句
1.数据的排序
order by 肯定是出现在sql语句最后
order by 排序标准 排序的形式(升序 asc,可以省略,默认的顺序;降序 desc,不可省略,反自然顺序)
按照工资排序 显示id,first_name
select id,first_name,salary from s_emp order by salary;
等价于
select id,first_name,salary from s_emp order by 3;
等价于
select id,first_name,salary from s_emp order by salary asc;
2.多字段排序 第一排序字段 第二排序字段
按工资排序 显示first_name ,salary
如果工资相同则按照first_name 降序
select id,first_name,salary from s_emp order by salary asc,first_name desc;
第一排序字段不同则按照第一排序字段排序,第一排序字段相同则启用第二排序字段规则
单行函数
单行函数:sql语句影响多少行,就针对每一行返回一个结果
select upper(first_name),first_name from s_emp;
select upper(first_name) from s_emp where salary>3500;
组函数:对一组数据处理之后得到一个结果
count 统计个数
select count(first_name) from s_emp;
select count(first_name) from s_emp where salary>1500;
select count(first_name) from s_emp where salary>3500;
测试当行函数----测试表-----dual
这个表是一个单行单列的表
单行函数:
1.字符串处理函数
upper-----把数据变大写
lower-----把数据变小写
initcap ----把字符串的每个单词首字母变大写
select initcap('one world one dream') from dual;
concat ----连接两个字符串
select concat('hello','world') from dual;
substr(参数1,参数2,参数3)
参数1 要截取的字符串
参数2 从什么地方开始截取 第一个是1
参数3 截取多长
select substr('hello',0,3) from dual;
select substr('hello',1,3) from dual;
第二个参数可以是负数
select substr('hello',-3,3) from dual;
显示first_name 的后三个字符,使用两种不同的方式完成
length -------得到字符串的长度
select length('hello') from dual;
select first_name,upper(substr(first_name,1,3)) from s_emp;