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 上面文件内容 ...
随机推荐
- 【DataBase】H2 DateBase的拓展使用
连接模式 支持以下连接模式: 嵌入模式(使用JDBC的本地连接) 服务器模式(使用JDBC或ODBC over TCP / IP进行远程连接) 混合模式(同时本地和远程连接) 嵌入模式 在嵌入模式下, ...
- array_map
<?php //对数组中的每个元素做函数处理 $arr = array(,,,,,); function cheng($hah){ ; } var_dump(array_map('cheng', ...
- linux信号量例子
semaphore.h 提供的是 POSIX 标准定义的 semaphore 接口,而 sys/sem.h 里 提供的是符合 System V 标准的 semaphore接口 (semget, sem ...
- [转]Office 安装卸载太麻烦?用这个工具帮你解决:Office Tool Plus
原文链接:https://sspai.com/post/43839 Office Tool官方网站:https://otp.landian.vip/zh-cn/ 真的很好用,发一个安装的截图:
- echarts移动端demo
说明:建议移动端使用的时候自己定制需要的东西,详情看官网 ECharts 效果图: 代码: <!DOCTYPE html> <html style="height: 1 ...
- axios ajax框架 请求配置
请求参数 { // `url` is the server URL that will be used for the request url: '/user', // `method` is the ...
- js 上传txt格式文件
判断文件是否为.txt格式: $(".delbao .file").on("change",function(){ var acceptType = $(thi ...
- django自带的用户验证是验证username和password,如何自定义验证功能,让mobil也能作为账户名登录?
users.views.pyfrom django.contrib.auth.backends import ModelBackendfrom django.db.models import Qcla ...
- [转帖]时间序列数据库 (TSDB)
时间序列数据库 (TSDB) https://www.jianshu.com/p/31afb8492eff 0.3392019.01.28 10:51:33字数 5598阅读 4030 背景 2017 ...
- 【Docker】:全面认识Docker和基本指令
Docker逐渐成为虚拟化技术的佼佼者,接下来会推出一系列的Docker相关技术文章,Docker技术也是架构师的必备技能. 什么是Docker Docker 是一个开源的应用容器引擎,基于Go语言, ...