mysql某个表的列除以2_数据库mysql(二)

数据库学习(mysql)

四. 数据查询(DQL:数据查询语言)

1. 基础查询

1. 多个字段查询:select 列名1,列名2 from student;

实例:select name,age from student;

2.去除重复:distinct

实例:select distinct name from student;

3.计算列:可以直接使用(+)如果是数字则直接相加,如果是字符串也可以相加

IFNULL(math,0)  第一个参数表示要替换的值,第二个表示替换后的值

意思是:如果math为null则把它替换为0

4.起别名:as(可省略)

实例:select name,english+IFNULL(math,0) as 总分 from student;

2. 条件查询

1. where后跟条件

2.运算符

>,=,<=,=,!=(<>)

and,or,not

between  and ,in

like

注意:null不能用=或!=连接 要用is

实例:select name from student where english is not null;

3.模糊查询(like)--重点

实例:select * from student where name like '马%';

查询姓马同学的信息

实例:select * from student where name like '_华%';

查询第二个字为华的同学

实例:select * from student where name like '%德%';

查询包含德同学的信息

解释:_表示一个任意字符,%表示一个或多个任意字符

3. 排序查询

1. select * from student order by math;

按数学成绩排序(desc为降序)

2.select * from student order by math desc,english desc;

如果数学成绩一样就按英语成绩降序排序

4. 聚合函数:将一列作为一个整体,进行纵向的计算的

1.count:计算个数

2.max,min:最大最小

3.sum:总和

4.avg:平均值

注意:聚合函数的计算会排除null值

实例:select avg(math) from student;  求数学成绩的平均值

5. 分组查询-分组后要么加分组后的字段,要么加聚合函数,加其他的字段没有意义

1. select sex,avg(score) ,count(id) from student group by sex;

查询男女生的平均分,及男女的人数;

2.select sex,avg(score) ,count(id) from student where score >70 group by sex;

查询分数大于70分的男女生平均分

3.where与having区别--重点

1.where 在分组前,having在分组后(where不满足条件不参与分组,having分组后再筛选)

2.where 不跟聚合函数的判断,having可以跟聚合函数

实例:select sex,avg(score) ,count(id)  as 人数 from student where score >70 group by sex having conut(id)/人数>2;

查询分数大于70分的男女生平均分后,再查询其人数大于2的数据

6. 分页查询(limit)

1. select * from student limit 0,3; 查询前3条数据

2. select * from student limit 3,3;查询第4-6条数据

不用数据库查看前n条数据方式不一样:

SELECT * FROM TABLE1 WHERE ROWNUM<=N  (oracle)

SELECT TOP N * FROM TABLE1   (sqlserver)

postgres与mysql一样

五.约束:对表中的数据,保证其正确性,有效性,完整性

1. 非空约束:not null

实例:create table stu (

id int,

name varchar(20) not null) --创建表添加约束

2. 删除表的非空约束

alter table stu modify name varchar(20);

3.创建表的非空约束

alter table stu modify name varchar(20) not null;

4.唯一约束:unique

实例:create table stu (

id int,

phone varchar(20) unique) --创建表添加约束

注意:如果为null值可以重复

5. 删除唯一约束

alter table stu drop index phone;  与删除非空约束不一样

6. 添加唯一约束

alter table stu modify phone varchar(20) unique;

7.主键约束:

1.非空且唯一

2.一张表只能有一个主键

实例:

create table stu (

id int primary key,

phone varchar(20) ) --创建表添加主键约束

3. 删除主键

alter table stu drop primary key;

4.添加主键

alter table stu modify phone id int primary key;

5.自动增长: 某一列为数字类型的,使用auto_increment

实例:

create table stu (

id int primary key auto_increment,

phone varchar(20) ) --创建表添加主键约束

6. 添加自动增长

alter table stu modify id int primary key auto_increment;

7.删除自动增长

alter table stu modify id int;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值