注意:这里()标记的内容为输入时需要加括号的,[]标记的内容输入时不需要加括号.
增加
创建新表:
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],...);
根据已有的表创建新表:
create table tab_new as select col1,col2... from tab_old definition only
插入内容:
insert into [表名] values (插入该表的内容);
插入一行的指定几列:
insert into [表名](所需要的列名) values (插入该表的内容)
查找
查找所有列的内容:
select * from [表名];
查找依据某个列:
select [列名] from [表名];
利用select来给某列起别名:
select [所需要的列名] as 别名;
面试高频考点:
排序使用关键词order by
select [列名] from [表名] order by [要排序的列];
在这里经常使用关键词desc(降序排列)
select [列名] from [表名] order by desc [要排序的列];
在这里也经常使用到关键词limit和offset.
limit关键字的用法: limit [数字]—表示取出第数字条数据;
limit [数字1],[数字2]—第一个数表示要跳过的数量,第二个数表示要取的数量;
使用的语句:
SELECT * from employees order by hire_date desc LIMIT 1
offset关键字的用法: offset [数字]—数字表示要跳过数据的条数;
select * from employees order by hire_date desc limit 1 offset 2;
条件查询使用关键词:where
select [列名] from [表名] where (条件);
范围查询
between,and,or(and和or相比,and比or更优先)
in(查询条件在某个具体的条件下)
in的使用方法eg:
select * from table1 where a [not] in ('值1','值2','值3','值6');
模糊查询(使用like)
使用统配符: %(代表剩余部分) _(代表一个字符)
总数:select count (*) as totalcount from table1;
求和:select sum(field1) as sumvalue from table1;
平均:select avg(field1) as avgvalue from table1;
最大:select max(field1) as maxvalue from table1;
最小:select min(field1) as minvalue from table1;
使用外连接
1 left outer join:
左外连接(左连接):结果集即包含连接表的匹配行,也包括左连接表的所有行
eg:select a.a,b.b,a.c,b.c,b.d,b.f from a left out join b on a.a=b.c;
2 right outer join:
右外连接(有连接):结果集既包含连接表的匹配行,也包括右连接表的所有行
3 full outer join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录
4 等值连接:无条件连接,取两个表的笛卡尔积,只返回两个表中联结相等的行(例如查找两张表)
```java
SELECT * FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号
两张关联表,删除主表中已经在副表中没有的信息
delect from table1 where not exists (select * from table2 where table1.field1=table2.field1);