mysql查询

 

1.表字段操作
1.语法:
alter table 表名 执行动作;
2.添加字段(add)(默认添加在最后一个字段)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 add 字段名 数据类型 after 字段名;
3.删除字段(drop)
alter table 表名 drop 字段名;
4.修改字段的数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
5.字符类型宽度和数值类型宽度的区别
1.数值类型宽度为显示宽度,只用于select 查询时显示,和存储无关,
可用zerofill查看效果
2.字符类型宽度超过之后无法存储
6.表重命名(rename)
alter table 原表名 rename 新 表名
7.表字段重命名(change)
alter table 表名 change 原名 新名 数据类型
练习:
1.创建一个库studb2
create database studb2
2.在 studb2中创建t1,字段有3个name age phnumber
use studb2;
create table t1(name varchar(15),age tinyint unsigned,phnumber int);
3.查看t1表的表结构
desc t1;
4.在t1表中第1个字段添加id字段,要求显示宽度为3,位数不够用0填充
alter table t1 add id int(3) zerofill first;
5.把phnumber的数据类型改为bigint
alter table t1 modify phnumber bigint;
6.在t1表中最后一个字段添加字段address
alter table t1 add address char(20);
7.删除表中age字段
alter table t1 drop age;
8.查看t1表的表结构
desc t1
2.数据类型
1.数值类型
2.字符类型
3.枚举类型
1.单选:enmu(值1,值2,值3...)
2.多选:set(值1,值2,值3...)
## 插入记录时:"girl,study,mysql"
4.日期时间类型
1.data : "YYYY-MM-DD"
2.time : "HH:MM:SS"
3.datatime : "YYYY-MM-DD HH:MM:SS"
timestamp: "YYYY-MM-DD HH:MM:SS"
1.分类
1.now() :返回当前时间
2.curdate() : 返回日期
3.curtime() : 返回时间

4.year(date) : 取出年份
5.date(date) : 取出日期
6.time(date) : 取出时间
2.查询2018年10月8日用户充值的信息
select * from t7 where date(cztime)="2018-10-08";
3.查询2018年10月8日10:00-12:00充值信息
select * from t7 where time(cztime)<="12:00:00"
and time(cztime)>="10:00:00";
4.查找2018年10月份用户充值信息
select * from t7 where date(cztime)>="2018-10-01"
and date(cztime)<="2018-10-31";
5.日期时间计算
1.语法格式
select * from 表名 where 字段名 运算符(now()-interval 时间间隔单位);

时间间隔单位:
1 day | 2 hour | 3 year | 3 month
2.示例
1.查询1天以内的充值记录
select * from t7 where cztime>=(now()-interval 1 day);
2.查询1年以前的用户充值记录
select * from t7 where cztime<(now()-interval 1 year);
3.查询1天以前,3天以内的充值记录
select * from t7 where cztime<=(now()-interval 1 day)
and cztime>=(now()-interval 3 day);
5.注意:
1.datatime : 不给值默认返回NULL
2.timestamp : 不给值默认返回系统当前时间
insert into t7(cztime) values(now()); ##返回当前时间
6.示例
1.创建用户充值表
create table t7(id int,
username varchar(20),
password varchar(20),
money int,
birthday date,
cztime timestamp)character set utf8;
3.表记录管理
1.删除表记录(delete)
1.delete from 表名 where 条件;
##不加where 条件,全部删除表记录
2.更改表记录(update)
1.update 表名 set 字段名1=值1,字段2=值2 where 条件;
##不加where条件则全部更改
3.练习
1.查找所有蜀国英雄信息
select * from hero where country='蜀国';
2.查找所有女英雄的姓名,性别和国家
select name,sex,country from hero where sex="女";
3.把id位2的记录改为典韦,性别男,国家改为魏国
update hero set name="典韦",sex="男",country="魏国" where id=2;
4.删除所有的蜀国英雄
delete from hero where country='蜀国';
5.把貂蝉的国籍改为魏国
update hero set country="魏国" where name="貂蝉";
6.删除所有的表记录
4.运算符操作
1.数值比较&&字符比较&&逻辑比较
1.数值比较: = != > >= < <=
2.字符比较:= !=
3.逻辑比较:and or between 值1 and 值2
where id between 100 and 200;
where id>=100 and id<=200;
4.示例
1.找出攻击力高于200 的蜀国英雄名字和攻击力
select name,gongji from sanguo where gongji>200 and country="蜀国";
2.将吴国英雄中攻击力为110的英雄攻击力改为100,防御改为60
update sanguo set gongji=100,fangyu=60 where gongji=110 and country="吴国";
3.查找蜀国和魏国的英雄信息
select * from sanguo where country="吴国" or country="魏国";
2.范围内比较
1.where 字段名 in(值1,值2,值3..)
2.where 字段名 not in(值1,值2,值3)
3.示例
1.查找攻击值在100-200之间的蜀国英雄信息
select * from sanguo where (gongji between 100 and 200
and country="蜀国");
2.找到蜀国和吴国以外的国家的女英雄信息
select * from sanguo where country not in("蜀国","吴国") and sex="女";
3.找到id为1,3,5的蜀国英雄和貂蝉的信息
select * from sanguo where id in(1,3,5) and country="蜀国" or name="貂蝉";
3.匹配空、非空
1.where name is NULL
2.where name is not NULL
3.示例
1.查找姓名为NULL的蜀国女英雄信息
select * from sanguo where name is NULL and country="蜀国" and sex="女";
4.注意
1.NULL:空值,只能用is、is not 去匹配
2."": 空字符串,用=、!=去匹配
5.模糊查询(like)
1.where 字段名 like 表达式
2.表达式
% : 匹配0到多个字符
_ : 匹配1个字符
3.示例
select * from sanguo where name like "_%_";
select * from sanguo where name like "%";
select * from sanguo where name like "__";
select * from sanguo where name like "赵%";
6.SQL查询
1.总结
3. select ...聚合函数 from 表名
1. where ....
2. group by ....
4. having ....
5. order by ....
6. limit ....;
2.order by :给查询的结果进行排序
1. ...order by 字段名 ASC(升序默认)/DESC(降序)
2.示例
1.将所有的英雄按防御值从高到低排序
select * from sanguo order by fangyu DESC;
2.将蜀国英雄按攻击值从高到低排序
select * from sanguo where country="蜀国"
order by gongji DESC;
3.将魏蜀两国英雄中名字为3个字的英雄按防御值升序排列
select * from sanguo where country in("魏国","蜀国")
and name like "___" order by fangyu;
3.limit(永远放在SQL语句的最后)
1.用法
1.limit n : 显示n条记录
2.limit m,n : 从第m+1条记录开始,显示n条
limit 2,3 : 显示3,4,5三条记录
2.示例
1.在蜀国英雄中查找防御值倒数第2名至倒数第4名的英雄记录
select * from sanguo where country="蜀国"
order by fangyu ASC limit 1,3;
2.在蜀国英雄中,查找攻击值前3名且名字不为NULL的英雄的姓名、攻击值和国家
select name,gongji,country from sanguo
where name is not NULL and country="蜀国"
order by gongji DESC limit 3;
3.分页
每页显示5(m)条记录,显示第4(n)页的记录
limit 15,5
limit (n-1)*m,m
4.聚合函数
1.分类
avg(字段) : 平均值
max(字段) : 最大值
min(字段) : 最小值
sum(字段) : 求和
count(字段) : 统计该字段记录的条数
2.示例
1.攻击力最强值是多少
select max(gongji) as max from sanguo;
2.统计id、name两个字段分别有几条记录
select count(id),count(name) from sanguo;
select count(*) from sanguo;
3.练习
1.统计蜀国英雄中攻击值大于200的英雄数量
select count(gongji) from sanguo
where gongji>200 and country="蜀国";
5.group by : 给查询的结果进行分组
1.示例
1.计算每个国家的平均攻击力
select country,avg(gongji) from sanguo
group by country;
2.查找所有国家的男英雄中英雄数量最多的前两名的国家名称及英雄数量
select country,count(*) as num from sanguo
where sex="男"
group by country
order by num DESC
limit 2;
2.注意
1.group by 后字段名必须要为select后的字段
2.如果查询字段和group by 后字段不一致,则必须对该字段进行
聚合处理(聚合函数)

6.having : 对分组聚合后的结果进行进一步筛选
1.示例
找出平均攻击力大于105的国家的前两名,显示国家名称和平均攻击力
select country,avg(gongji) from sanguo
group by country
having avg(gongji)>105
order by avg(gongji) DESC
limit 2;
2.注意
1.having 语句通常与group by 联合使用
2.having语句存在弥补不了where关键字不能与聚合函数联合使用的不足,
where只能操作表中实际存在的字段,having操作的是聚合函数生成的显示列

 

转载于:https://www.cnblogs.com/yuzhoudiyishuai/p/10720323.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值