Mysql语句练习记录
使用的sql图形软件:SQLyogEnt
使用的数据库:MYSQL5.7
软件地址:
链接:https://pan.baidu.com/s/1lajyXaSnmrO1v5v987NOoA
提取码:i3a4
-------------------------------------------------------------------------------------------------------------------------------------
//创建名为student的数据库
create database student;
//显示数据库是否创建成功
show databases;
//跳转到自己想要做修改的表,指定操作的数据库名为student
use student;
//创建stu表,在创建时指定属性和属性类型,并给于这个属性指定大小
create table stu(id int(4),name varchar(12),age int(10),sex char(2),birthday date);
//显示是否创建表成功
show tables;
//显示数据库stu表的基础信息
desc stu;
//插入数据,varchar和char还有date类型数据使用‘’括起来
insert into stu values(1,'we',11,'n','2019-11-12');
//插入数据有两种方式,一种是指定属性插入和全部属性插入
insert into stu(id,name) values(1,'ju');
insert into stu values(2,'w',11,'n','2019-11-12'),(3,'w',11,'n','2019-11-12'),(4,'w',11,'n','2019-11-12');//连着插入三条数据
//删除student数据库
drop database student;
//删除stu表
drop table stu;
//修改数据库表的信息
update stu set name='jing' where age=11;
update stu set name='wan',age=18 where id=1;
//删除
delete from stu where id=1;
delete from stu where name='li' and age=18;
//查询
select * from product;//查询所有
select name,price from product;
select name,price+10 from product;//查询所有价格加十后的显示结果
select p.name,p.price from product AS p;//给表起别名,多用于多表查询
select name,price+10 AS "产品的新价格" from product;//给列起别名
select * from product where name="华为电脑001";//查询条件为name="华为电脑001"的全部记录
select *from product where price!=23;//价格不等于23的所有记录
select *from product where price>23 and price<100;//查找价格23到100之间的记录
select * from product where price between 23 and 100;//查找价格23到100之间的记录,包含23
select * from product where price = 23 or price=100;//价格等于23或100的所有记录
select * from product where price in(23,100);//等同于上一句
//碰到关键字在输入的名称左边加顿号
select distinct type from product;//查找所有type并使用distinct去除重复
select * from product where type is null;//查询出没有分类的所有记录
select * from product where type is not null;//查询出有分类的所有记录
select * from product order by price asc;//按照价格的大小升序排序
select * from product order by price desc;//按照价格的大小降序排序
//聚合函数
select count(*) AS "总数" from product;//统计有多少条记录,并起个别名(效率低不建议使用)
select count(1) as "总数" from product;//建议这样使用 1 代表只遍历统计下标为1的属性
select sum(price) from product;//查询出所有的价格总和
select max(price) from product;//查询价格最高
select min(price) from product;//查询最低价格
select avg(price) from product;//查询价格的平均值
select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' from product;
select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' ,count(1) as '总记录数'from product;
模糊查询
select *from product where name like '%电%';//%代表匹配一个或者多个字符,_ 只匹配一个字符
分组操作
select * from product group by type;//根据type进行分组,分组后重复会被去掉
分组后进行过滤,筛选
select * from product group by type having type is not null;
Mysql语句练习记录的更多相关文章
- MySQL语句学习记录
注意,命令行下,每条语句最后都需要加分号. 1.显示所有数据库 SHOW DATABASES 2.使用某数据库 如mysql数据库 use mysql (sql语句不区分大小写) 3.显示所有表 ...
- mysql语句:批量更新多条记录的不同值[转]
mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 帮助 1 UPDATE mytable SET myfield = 'value' WHERE ...
- mysql语句:批量更新多条记录的不同值
mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 1 UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_va ...
- Mysql中 查询慢的 Sql语句的记录查找
Mysql中 查询慢的 Sql语句的记录查找 慢查询日志 slow_query_log,是用来记录查询比较慢的sql语句,通过查询日志来查找哪条sql语句比较慢,这样可以对比较慢的sql可以进行优化. ...
- mysql语句分析
explain的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 1,id select识别符.这是select的查询序列号.2,select_type 可以为一下任何一种类型simple ...
- MySQL高效获取记录总数
通常mysql获取查询记录总数我们使用如下语句: SELECT COUNT(*) FROM users WHERE k='avs'; 或:SELECT id FROM goods WHERE k=' ...
- CentOS7.4安装MySQL踩坑记录
CentOS7.4安装MySQL踩坑记录 time: 2018.3.19 CentOS7.4安装MySQL时网上的文档虽然多但是不靠谱的也多, 可能因为版本与时间的问题, 所以记录下自己踩坑的过程, ...
- MySql基础笔记(二)Mysql语句优化---索引
Mysql语句优化--索引 一.开始优化前的准备 一)explain语句 当MySql要执行一个查询语句的时候,它首先会对语句进行语法检查,然后生成一个QEP(Query Execution Plan ...
- python 全栈开发,Day60(MySQL的前戏,数据库概述,MySQL安装和基本管理,初识MySQL语句)
一.MySQL的前戏 在学习Mysql之前,我们先来想一下一开始做的登录注册案例,当时我们把用户的信息保存到一个文件中: #用户名 |密码 root|123321 alex|123123 上面文件内容 ...
随机推荐
- 【Java】阿里巴巴Java开发手册
阿里巴巴Java开发手册 下载地址:https://github.com/alibaba/p3c 阿里巴巴代码规范检查插件p3c 下载地址:https://github.com/alibaba/p3c
- scons 简单入门
scons 简单入门 摘自:https://www.jianshu.com/p/e4bd3ab9e5d6 0.2042019.01.20 12:58:44字数 1201阅读 3021 简单入门 hel ...
- bat函数调用 带返回值
bat 脚本之 使用函数 摘自:https://blog.csdn.net/peng_cao/article/details/73999076 综述 bat函数写法 bat函数调用 bat函数返回值 ...
- Ubuntu18.04 instsall XMind_8 and crack
1.dowload XMind_8 linux install zip wget https://www.xmind.cn/xmind/downloads/xmind-8-update8-linux. ...
- LODOP打印table超宽用省略号带'-'的内容换行问题
前面的博文有div超宽隐藏(LODOP打印超过后隐藏内容样式),还有有table设置超宽隐藏(),此外,还有超宽后用省略号表示的css样式,此文是针对这个样式的.该样式正常情况下没问题,但是遇到-短线 ...
- angular2-cookie 如何升级到 ngx-cookie
angular2-cookie 如何升级到 ngx-cookie https://github.com/salemdar/angular2-cookie#readme
- python logging模块日志输出
import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = ...
- clickHouse可视化查询工具
clickHouse以卓越的查询性能著称,目前在大数据的存储和分析领域有广泛应用,目前TreeSoft已支持clickHouse的数据在线查询分析,可以与Mysql,oracle等数据库并存操作. 1 ...
- beego框架(golang)学习过滤器(实现restful请求)
过滤器 在用beego做restful路由的时候,遇到了除了GTE.POST之外的HTTP请求,比如 PUT.PATCH.delete请求无法通过路由认证,报错误:405 METHOD NOT ALL ...
- Linux chomd命令
file 语法为: chmod abc file 其中a,b,c各为一个数字,分别表示User.Group.及Other的权限. r=4,w=2,x=1 若要rwx属性则4+2+1=7: 若要rw-属 ...