14、多表查询
select __,__ from __,__;//会容易产生笛卡尔集,比如要查询的a表中的m行,b表中的n行//最后结果是m*n行,显然不是要的结果//笛卡尔集的产生条件为:省略连接条件;连接条件无效;所有表中的所有行互相连接//为了避免产生笛卡尔集,可以在where中加入有效的连接条件//(1)等值连接select beauty.id Name boyName from beauty,boys where beauty,boys where beauty.boyfriend_id=boys.id;//(2)表的别名select bt.id Name boyName from beauty bt,boys b where bt.boyfriend_id=b.id;//(3)连接多个表//连接n个表,至少需要n-1个连接条件//join连接//内连接 [inner] join on//外连接 左 left [outer] join on// 右 right [outer] join onselect bt.id,Name,boyName from beauty bt innerjoin boys b on bt.boyfriend_id=b.id;//(4)如果是多表连接select employee_id,city,department_name
from employees e
join department d
on d.department_id=e.department_id
join locations l
on d.location_id=l.location_id;
15、常见函数
//字符函数、数学函数、日期函数、其他函数、流程控制函数
lower //小写
upper //大写//字符控制函数
concat('lycdsg','handsome')//lycdsghandsome
substr('lycdsg',1,5)//lycds
length('lycdsg')//6
instr('lycdsg','s')//5
lpad('name',10,'*')//****lycdsg
rpad('name',10,'*')//lycdsg****
trim('l'from'lycdsg')//ycdsgreplace('lycdsg','d','h')//lychsg//数字函数round(37.848,2)//37.85 四舍五入truncate(45.953,2)//45.95 截断mod(1600,300)//100 求余//日期函数
now //获取当前日期
str_to_date //将日期格式的字符转换成指定格式
str_to_date('3-12-1999','%m-%d-%Y')//1999-03-12
date_formate //将日期转换成字符
date_formate('2019/2/3','%Y年%m月%d日')//2019年02月03日//条件表达式//case表达式select last_name,job_id,salary,case job_id
when __ then __
when __ then __
else __
end 新增列名
from employees;
16、增、删、改
(1)insertinsertintotable(___)values(__);//一次只能向表中插入一条数据//向表中插入空值//隐式方式insertinto customer(id,age)values('10004',30);//显示方式insertinto customer
values('10000',25,null,null);//插入指定值now()//记录当前系统的日期和时间//从其他表中copy数据insertinto customer
select*from __ where ____;(2)updateupdate __
set __ __
where __;(3)deletedeletefrom __
where __;