Sql常用语法总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34083066/article/details/87110442

目录

查询select

添加insert

修改update

删除delete

去重查询distinct

条件查询where子句

and&or运算符

排序order by

模糊查询like

通配符%和_

范围值查询in

中间范围值查询between and

设置别名 as(可省略)

添加表(表备份)

Oracle中使用select into 

MySql中使用insert into select

null

查询不为空not null

查询为空 null

sql运算符

查询限定

mysql中使用limit

oracle中使用rownum

关联查询

inner join等价于 join

left join 左外连接

right join 右外连接

日期查询

mysql使用date_format

oracle使用to_date

分组查询group by

创建表

alter修改

删除操作

视图

索引

 

 


查询select

select * from table_name;

 

添加insert

//指定字段
insert into table_name(c1,c2) values (v1,v2);
//全部字段,这里必须每个值都要传
insert into table_name values(v1,v2......);

 

修改update

注意必须指定where子句,不然会更改全表。重大事故!!!

//多字段使用逗号隔开
update table_name set c1 = v1,c2 = v2 where c = v;

 

删除delete

注意必须指定where子句,不然会更改全表。重大事故!!!

delete from table_name where c =v;

 

去重查询distinct

查询出列值不相等的符合条件的内容

select distinct c1 from table_name;

 

条件查询where子句

之前的update和delete都使用过了。

select * from table_name where c = v ;

 

and&or运算符

配合where使用判断多个条件。

select * from table_name where c1 = v1 and c2 = v2 or c3 = v3;

 

排序order by

默认升序排序ASC;可以使用DESC进行降序排序

可以指定多列排序,指定多列排序时,先排序第一列,再排序第二列,以此类推。

select * from table_nae group by c1,c2 desc;

 

模糊查询like

配合通配符使用,进行模糊匹配。

通配符%和_

%:匹配多个字符

_ 匹配一个字符

查询wm字符前有一个字符,后有多个字符的情况
select * from table_name c like '_wm%'

 

范围值查询in

语序指定多个值。

select * from table_name where c in (v1,v2);

 

中间范围值查询between and

select * from table_name where c1 between v1 and v2;

 

设置别名 as(可省略)

select a.c1,a.c2 from table_name as a;

 

添加表(表备份)

Oracle中使用select into 

select * into table_name_bak from table_name;

MySql中使用insert into select

insert into table_name_bak select * from table_name; 

null

查询不为空not null

select * from table_name where c1 is not null;

查询为空 null

select * from table_name where c1 is null;

sql运算符

=    等于
>    大于
<    小于
<>    不等于
>=    大于等于
<=    小于等于

查询限定

mysql中使用limit

select * from table_name limit 5;

oracle中使用rownum

select * from table_name where rownum <= 5;

关联查询

inner join等价于 join

inner join中的inner可以省略,使用他关联时,左右都有数据的展示。

select a.c1,b.c2 from table_name1 a join table_name2 b where a.c3 = b.c3;

left join 左外连接

无论右表是否有匹配数据,左边列也展示。

select a.c1,b.c2 from table_name1 a left join table_name2 b where a.c3 = b.c3;

right join 右外连接

无论左边是否存在匹配数据,右边列也展示。

select a.c1,b.c2 from table_name1 a right join table_name2 b where a.c3 = b.c3;

日期查询

mysql使用date_format

select * from table_name where DATE_FORMAT(cDate,'%Y-%m-%d %H:%m:%s') >DATE_FORMAT('2019-02-06 12:12:12','%Y-%m-%d %H:%m:%s');

oracle使用to_date

select * from table_name where to_date(cDate,'yyyy-mm-dd hh:mm:ss') >DATE_FORMAT('2019-02-06 12:12:12','yyyy-mm-dd hh:mm:ss');

分组查询group by

配合聚合函数使用,当再分组完成前,无法使用where子句。所以使用having来进行条件查询。

select c1,count(*) from table_name group by c1 having c1 > 5;

创建表

这里举个例子,几乎可以把建表这一块的内容说的差不多了。

create table student(
s_id int not null AUTO_INCREMENT,
s_name VARCHAR(32) not null UNIQUE,
s_age int(2),
s_class int(2),
PRIMARY key (s_id),
FOREIGN key (s_class) REFERENCES class(c_id)
)

alter修改

//添加列
alter table table_name add c1 VARCHAR(32);
//删除列
alter table table_name drop column c1;
//更改列数据类型
alter table table_name modify column c1 varchar(32);

删除操作

//删表
drop table table_name;
//删库
drop database database_name;
//清空表
truncate table table_name;

视图

视图是一个虚拟的表,便于查询

create view view_name as select c from table_name where c = v;

索引

传送门:MySql数据库索引

猜你喜欢

转载自blog.csdn.net/qq_34083066/article/details/87110442