SQL语句分类
1、数据定义语言DDL
 CREATE,DROP,ALTER对逻辑结构等有操作的,其中包括表结构,视图和索引
2、数据查询语言DQL 
 各种简单查询,连接查询等
3、数据操纵语言DML 
 对数据进行操作的,数据操纵分成数据查询和数据更新两类
4、数据控制功能DCL 
 GRANT,REVOKE,COMMIT,ROLLBACK主要为以上操作 即对数据库安全性完整性等有操作的,可以简单的理解为权限控制等。
查询
关联查询
交叉连接cross join,很少使用
 内连接 inner join,简写为join
 外连接:left join/right jion
 联合查询(union、union all):前提,两张表的列数要一致,union会合并相同的行,union all不会
子查询
条件:一条SQL语句的查询结果做为另一条查询语句的条件或查询结果
 嵌套:多条SQL语句嵌套使用,内部的SQL查询语句称为子查询。
 
1、子查询是单行单列的情况:结果集是一个值,父查询使用:=、 <、 > 等运算符
 – 查询工资最高的员工是谁?
 select  * from employee where salary=(select max(salary) from employee);   
 
2、子查询是多行单列的情况:结果集类似于一个数组,父查询使用:in 运算符
-- 查询开发部与财务部所有的员工信息
select * from emp where dept_id in (select id from dept where name in("开发部","财务部")); 
-- 查询工资大于 5000 的员工,来自于哪些部门的名字
select name from dept where id in (select dept_id from emp where salary > 5000); 
 
3、子查询是多行多列的情况:结果集类似于一张虚拟表,不能用于where条件,用于select子句中做为子表
 – 1) 查询出2011年以后入职的员工信息-- 2) 查询所有的部门信息,与上面的虚拟表中的信息比对,找出所有部门ID相等的员工。select * from dept d, (select * from employee where join_date > ‘2011-1-1’) e where e.dept_id = d.id;
– 使用表连接:select d., e. from dept d inner join employee e on d.id = e.dept_id where e.join_date > ‘2011-1-1’
常用sql语句
查
查询:Select * from table_name
 字段查询:select fileds from table_name
 条件查询:Select * from table_name where xx
 排序: Select * from table_name order by x sec
 分页:Select * from table_name limit 0,10
 去重:Select distinct(x) from table_name
条件查询
 比较:= > < <>(不等于)
 通配:like
 范围限定 :and
 子集限定:in
 逻辑关系:and or not
聚合查询
 语法:group by 字段 having 条件
 常用函数 count,max,min,sum,avg
select count(字段) from 表 group by 字段
 select count(字段) from 表 group by 字段 having 字段=’xx’
LIMIT n 等价于 LIMIT 0,n。
 
增
Insert into 表(字段)valuse(数据)
LOAD DATA LOCAL INFILE 'dump.txt' INTO TABLE mytbl;
 
改
Update 表 set 字段=赋值 where xx
删
Delete from 表 where 条件










