2018-04-01

追加一下数据库的基本操作

修改

update 表名 set 列名='内容'... where 条件

修改值

update 表名 set 列名='内容' where id between 开始 and 结束

删除

删除列:alter table 表名 drop  列名

删除行:delete from  表名  where  条件 (id=2)

查询(条件)

比较运算符

等于=

大于>

大于等于>=

小于<

小于等于<=

不等于!=或<>

select * from   表名 where   条件 如(id>3)

逻辑运算符

and

or

not

select * from 表名 where 条件 如(id>3 and gender=0);

模糊查询

like

%表示任意多个任意字符

_表示一个任意字符

select * from 表名 where 条件 如(名字 like '黄%');

范围查询

in表示在一个非连续的范围内

select * from 表名 where 条件 如(id in(1,3,8));

select * from 表名 where 条件 如(id between(在) 3 and(和) 8 and gender=1;)

select distinct 字段名 from 表名; 查看时消除重复项

select * from 表名 where 条件; 查看其中一行

空判断

注意:null与''是不同的

判空is null

select * from 表名 where 条件 如(名字 is(是) null(不存在的));

查询(聚合)

查总和

select count(*) from 表名;

查最大值

select max(id) from 表名

查最小值

select min(id) from 表名

查总和

select sum(id) from 表名

查平均值

select avg(id) from 表名 where isdelete(是否删除)=0 and gender(性别)=0;

分组

select 字段 as 别名,count(*)

from 表名

group by 字段;

select 字段 as 别名,count(*)

from 表名

group by 字段

having(条件) gender=1;

查询(排序)

语法

select * from 表名

order by 列1 asc|desc,列2 asc|desc,...

升序

select * from 表名

where (条件)isdelete=0

order by stitle;

查询(消除重复行)

select distinct 字段 from 表名

select distinct* from rg_201 where id < 5  order by id desc;

查询(分行)

语法

select * from 表名

limit start(起始)1,count(出现几个)5

优先级

小括号,not,比较运算符,逻辑运算符

and比or先运算,如果同时出现并希望先算or,需要结合()使用

select distinct *    清除重复段

from                    表名

where ....             条件

group by ...          分组having ... 

order by ...           排序

limit star,count      分页

链接查询

select 学生表.字段名,科目表.字段名,成绩表.字段名

from 成绩表

inner join 学生表 on 成绩表.主键 = 学生表.主键

inner join 科目表 on 成绩表.主键 = 科目表.主键


select qwe.qname

    -> from asd

    -> inner join qwe on asd.a_id = qwe.id;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容