SQL语法简介
SQL 是结构化查询语言的简称(STRUCTED QUERY LANGUAGE).
SQL 分为DQL、DML和DDL三种
DQL: 数据查询语言 基本就是select查询
DML: 数据操作语言 insert、update、delete
DDL:数据定义语言 创建、删除、修改表、索引等数据
DDL
建表语句:1
2
3
4create table table_name (
field_name, varchar(40),
....
)
删表语句1
drop table table_name;
DML
插入
1 | insert into table_name (field1,field2,...) values (val1,val2); |
更新
1 | update talbe_name set field1 = val1, field2=val2 .... |
删除
1 | delete from table_name where .... |
DQL
1 | select * from table_name .... |
过虑条件
where语句1
select * from table_name where field1=va1;
排序
Order by语句1
select * from table_name order by field1;
分组
Group by分组1
select * from table_name
join
left join、right join、union join等
其他SQL语句
insert into …select
insert into …select 可以把数据从一张表的数据插入到另一张表中
union语句
union可以将从两张表中查询出来的语句整合到一张表中1
select * from student where no = 1 union select * from student_bak where no =2;
union可以把结果集中相同的的两条记录合并成一条。
如果不想合并,请使用union all