一、基本语句
- 创建数据库
CREATE DATABASE databasename
- 删除数据库
drop database dbname
- 创建表
新表:create table tabname(,,,)
创建已有表:create table tab_new like tab_old
,
create table tab_new as select col1,col2… from tab_old definition only
- 删除表
drop table tabname - 增加一列
Alter table tabname add column col type
- 添加主键
Alter table tabname add primary key(col)
- 删除主键
Alter table tabname drop primary key(col)
- 创建索引
create [unique] index idxname on tabname(col….)
- 创建视图
create view viewname as select statement
- 增加
insert into table1(field1,field2) values(value1,value2)
- 删除
delete from table1 where 范围
- 修改
update table1 set field1=value1 where 范围
13.查询
select * from table1 where field1 like ’%value1%’
二、单表查询
2.1基本数据查询
- 查询所有字段数据
select * from 表名
- 查询部分数据
select name,gender,age from 表名
- 查询去重字段
select distinct age from 表名
2.2条件数据查询
- 查询指定记录
select * from 表名 where name = 'lihua'
- 范围查询
select name from 表名 where id in (001,002,003)
- 区间查询
select name from 表名 where chese between 85 and 90
- 模糊查询
select name from 表名 where name like 'L%'
- 通配符查询
select name from 表名 where name '_L%'
- 非空查询
select name,chese from 表名 where chese is not null
- and多条件查询
select name,chese from 表名 where name like 'a%' and chese < 90
- or多条件查询(满足一个即可被查出来)
select name,chese from 表名 where name like 'a%' or chese < 90
- 对查询结果进行统计
select name chese from 表名 order by chese ASC
ASC表示升序,DESC表示降序,默认升序,多字段排序 order by chese ASC,English DESC
2.3统计函数和分组查询
- count()记录条数(非空行)
所有数据记录:select count(*) number from 表名
指定字段记录:select count(name) number from 表名 where chese < 90
- avg()计算字段平均值
select avg(chese) avgerage from 表名
- sum()求和字段总值
select sum(chese) from 表名
- max(),min()字段最值
select max(chese),min(chese) from 表名
- group by分组查询
select * from 表名 group by subject
- having 限定分组查询
在MySQL中不能同where语句对分组语句进行条件限制,需要having语句来实现
select name, age, avg(salary) average from 表名 group by age having avg(salary) >8000
- limit限定查询条数
select * from 表名 where salary >8000 limit 3
select * from 表名 where salary >8000 limit 1,3 #(从第二条开始,限制3条)
三、多表查询
3.1内连接查询
- 自连接:表与其自身连接
select ts1.stuid,ts1.name,ts1.classno from t-student as ts1,t-student as ts2 where ts1.classno = ts2.classno and ts2.name = 'alica'
- join in 自连接
select ts1.stuid,ts1.name,ts1.classno from t-student as ts1 join in t-student as ts2 on ts1.classno = ts2.classno and ts2.name = 'alica'
3.2外连接查询
- 左外连接:左边全部显示,右边根据on语句筛选匹配
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
- 右外连接:右边全部显示,左边根据on语句筛选匹配
select a.a, a.b, a.c, b.c, b.d, b.f from a RIGHT OUT JOIN b ON a.a = b.c
- UNION并操作
select * from t1 union select * from t2
3.3子查询
- 子查询
通俗的讲,子查询就是查询里还有查询语句
如select * from t1 where column1 = (select column1 from t2);
select * from t1 where id >= (select id from t2 where id = 3);
- 子查询指嵌套在 查询内部,且必须始终出现在 圆括号内。
- 子查询可以包含多个关键字或者条件,如
DISTINCT,GROUP BY,ORDER BY,LIMIT,
函数等 - 子查询的外层查询可以是:
SELECT,INSERT,UPDATE,SET,DO
- 子查询的返回值可以是:标量、一行、一列,甚至是另一个子查询
- 带关键字的子查询
IN: 若条件想要落在子条件的范围里可以使用IN
select O_id,POL,POD from s_order where C_id in (select C_id from client where C_name like '佐%');
exits:子查询存在时才能进行外查询
select * from S_order where CNTR_Q > 2 AND exists (select c_ID from s_order where pod='RIGA');
any:any表示满足其他任意一个条件,>any表示大于任意一个值
select * from s_order where S_teu > any (select Tue_avg from Past_avg);
all:表示满足所有条件,>all 表示大于所有值
select * from s_order where S_teu > all (select Tue_avg from Past_avg);