SQL 语句大全
DDL
-
操作库
-
创建库
- create database 库名 ;
-
创建库是否存在,不存在则创建
- create database if not exists 库名 ;
-
查看所有数据库
- show databases;
-
查看某个数据库的定义信息
- show create database 库名;
-
修改数据库字符信息
- alter database 库名 charcter set utf-8;
-
删除数据库
- drop database 库名;
-
-
操作表
-
创建表
-
create table 表名(
字段名1 字段类型,
字段名2 字段类型,
字段名3 字段类型
);
-
-
查看表结构
-
-
desc 表名;
-
修改表名
- alter table 原表名 rename to 新表名;
-
-
查看创建表的SQL语句
-
show create table 表名 ;
-
添加一列
- alter table 表名 add 列名 数据类型;
-
删除列
-
-
alter table 表名 drop 列名;
-
删除表
- drop table 表名;
- drop table if exists 表名;
DML
- 增(insert into)
- 给指定列插入数据
- insert into (列名1,列名2,…) values(值1,值2,…);
- 给所有列添加(不写列名)
- insert into values(值1,值2,…);
- 给指定列插入数据
- 删(delete)
- 删除表中数据
- delete from 表名 where 列名 =值 ;
- 删除表中所有数据
- delete from 表名;
- 删除表中所有数据,并创建一个一模一样的空表(高效)
- truncate table 表名;
- 删除表中数据
- 改(update)
- 不带条件的修改(会修改所有行)
- update 表名 set 列名 =值;
- 带条件的修改
- update 表名 set 列名 = 值 where 条件;
- 不带条件的修改(会修改所有行)
+++
DQL
-
基础关键字
-
between…and (两边都包括)
-
-- 查询年龄大于等于20岁小于等于30的人 select * from student where age>=20 && age<=30; select * from student where age>=20 andage<=30; select * from student where age between 20 and 30 ; -- 查询年龄为22,18,20的人 select *from student where age=22 or age=18 or age=20; select *from student where age in (20,18,22);
-
-
is not null(不为空) like(模糊查询) distinct(去重复值)
-
-- 查询英语成绩不为null select * from student where english is not null; -- _:表示单个任意字符 %: 表示任意字数的任意字符 -- 查询姓马的人有哪些 select * from student where name like '马%'; -- 查询姓名第二个字是花的人 select * from student where name like '_花%'; -- 关键词 distinct 用于返回唯一不同的值 select distinct name from student;
-
-
-
排序查询 (order by)
-
-- 例子 select * from student order by math; -- 默认为升序 asc select * from student order by math desc;-- 降序排序
-
-
聚合函数:将一列数据作为一个整体,进行纵向的计算。
- count:计算个数
- max:计算最大值
- min:计算最小值
- sum:计算和
- avg:计算平均数
-
分组查询(group by)
-
-- 按照性别分组。分别查询男、女同学的数学平均分 select sex,avg(math) from student group by sex; -- 按照性别分组。分别查询男、女同学的平均分,人数 select sex,count(id),avg(math)from student grout by sex; -- 按照性别分组。分别查询男、女同学的平均分,人数 要求:分数低于70分的人,不参与分组 select sex,count(id),avg(math)from student where math>=70 grout by sex; -- 按照性别分组。分别查询男、女同学的平均分,人数 要求:分数低于70分的人,不参与分组,分组之后。人数要大于2个人 select sex,count(id),avg(math)from student where math>=70 grout by sex having count(id)>2;-- 优先关系:where>group by>having。
-
-
分页查询
-
语法:limit 开始的索引 ,每页要查询的条数;
-
tips:开始的索引=(当前的页数-1)*每页要查询的条数
-
limit是MySQL中的方言 在别的SQL语句中不可用。
-
-- 每页显示3条记录 select * from student limit 0,3; -- 第一页 select * from student limit 3,3;-- 第二页 select * from student limit 6,3;-- 第三页
-
-
-
内连接查询
-
1. 从哪些表中查询数据
2.条件是什么
3. 查询哪些字段
-
隐式内连接:使用where条件消除无用数据
-- 查询员工表的名称,性别,部门表的名称 select emp.name,emp.sex,dept.name from emp,dept where emp.dept_id=dept.id;
-
显示内连接:
-- 语法:select 字段列表 from 表名 [inner] join 表名2 on 条件 inner可省略 -- 查询员工表的名称,性别,部门表的名称 select emp.name,emp.sex,dept.name from emp inner join dept on emp.dept_id=dept.id;
-
-
-
外连接查询
-
左外连接:查询的是左表所有数据以及其交集部分
-
-- 语法:select 字段列表 from 表1 left [outer] join 表2 on 条件; -- 例子: -- 查询所有员工信息,如果员工有部门,则查询部门名称,没有部门,则不显示部门名称 select emp.*,dept.name from emp left join dept on emp.dept_id=dept.id;
-
-
右外连接 – 查询的是右表所有数据以及其交集部分
-
-- 语法: select 字段列表 from 表1 right [outer] join 表2 on 条件; -- 例子: -- 查询所有员工信息,如果员工有部门,则查询部门名称,没有部门,则不显示部门名称 select emp.*,dept.name from dept right join emp on emp.dept_id=dept.id;
-
-
-
子查询:查询中嵌套查询
-- 查询工资最高的员工信息 -- 1 查询最高的工资是多少 9000 select max(salary) from emp; -- 2 查询员工信息,并且工资等于9000的 select * from emp where emp.salary=9000; -- 一条sql就完成这个操作。这就是子查询 select * from emp where emp.salary=(select max(salary) from emp);
-
子查询的结果是单行单列的
子查询可以作为条件,使用运算符去判断。 运算符: > >= < <= =
-- 查询员工工资小于平均工资的人 select * from emp where emp.salary<(select avg(salary) from emp);
-
子查询的结果是多行单列的:
子查询可以作为条件,使用运算符in来判断
-- 查询'财务部'和'市场部'所有的员工信息 SELECT id FROM dept WHERE NAME = '财务部' OR NAME = '市场部'; SELECT * FROM emp WHERE dept_id = 3 OR dept_id = 2; -- 子查询 select * from emp where dept_id in (select id from dept where name in('财务部','市场部'));
-
子查询的结果是多行多列的:
子查询可以作为一张虚拟表参与查询
-- 查询员工入职日期是2011-11-11日之后的员工信息和部门信息 -- 子查询 select t1.*,t2.* from (select * from emp where join_date>'2011-11-11') t1,dept t2 where t1.dept_id=t2.id; -- 内连接 select emp.* ,dept.* from emp,dept where emp.join_date='2011-11-11' and emp.detp_id = detp.id;
-----示例内容来源:itheima
-