一、sql基础
日期和字符只能在单引号 中出现
在select子句中使用关键字 distinct 删除重复行
select distinct department_id
from employees;
使用 desc(describe)命令,显示表结构
desc employees;
sql是一种语言,sql plus是一种环境,mysql是数据库
二、过滤和排序数据
1.where过滤
where子句紧随from子句,使用where子句将不满足条件的行过滤掉
字符大小写敏感,日期格式敏感
默认的日期格式是DD-MON月-RR
SELECT last_name, hire_date, department_id
FROM employees
WHERE hire_date = '7-6月-1994'
建议用这一种↓↓↓
SELECT last_name, hire_date, department_id
FROM employees
WHERE to_char(hire_date,'yyyy-mm-dd') = '1994-06-07'
2.like
% 代表零个或多个字符(任意个字符)。
_ 代表一个字符。
%和_可以同时使用
select job_id
from jobs
where job_id like 'IT\_%' escape '\'
3.order by排序
使用order by子句排序
asc(ascend)升序-从小到大,默认是从小到大
desc(descend)降序-从大到小
order by子句在select语句的结尾
三、单行函数
1.字符函数
<1>大小写控制函数
lower-全部小写
upper-全部大写
initcacp-首字母大写
<2>字符控制函数
concat-连接字符串
substr-从第几个字符开始取,取几个字符(注意:是从1开始)
length-字符数
instr-首次出现的位置。若无返回0(在第一位返回1,也是从1开始)
lpad-补左边,补到几位,用什么补
rpad-补右边,同上
trim-将一个字符从另一个字符串中移除(注意:只能去首尾)
replace-将一个字符串中的某一个字符取代为另一个(注意:全部替换)
select concat('hello','world'),substr('helloworld',2,4),length('helloworld')
from dual;
运行结果↓↓↓
CONCAT('HELLO','WORLD') SUBSTR('HELLOWORLD',2,4) LENGTH('HELLOWORLD')
----------------------- ------------------------ --------------------
helloworld ello 10
select instr