mariadb 3
MariaDB第三章(select)
基本查询

--查询基本使用(条件,排序,聚合函数,分组,分页) --创建学生表
create table students (
id int unsigned not null auto_increment primary key,
name varchar(20) default '',
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('男', '女', '中性', '保密') default '保密',
cls_id int unsigned default 0,
is_delete bit default 0
); --创建班级表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(20) not null
); --往students表里插入数据
insert into students values
(0,'小明',18,180.00,1,1,0),
(0,'小月月',19,180.00,1,2,0),
(0,'彭于晏',28,185.00,1,1,0),
(0,'刘德华',58,175.00,1,2,0),
(0,'黄蓉',108,160.00,2,1,0),
(0,'凤姐',44,150.00,4,2,1),
(0,'王祖贤',52,170.00,2,1,1),
(0,'周杰伦儿',34,null,1,1,0),
(0,'程坤',44,181.00,1,2,0),
(0,'和珅',55,166.00,1,2,0),
(0,'刘亦菲',29,162.00,2,3,0),
(0,'金星',45,180.00,3,4,0),
(0,'静香',18,170.00,2,4,0),
(0,'郭靖',22,167.00,1,5,0),
(0,'周杰',33,178.00,1,1,0),
(0,'钱小豪',56,178.00,1,1,0),
(0,'谢霆锋',38,175.00,1,1,0),
(0,'陈冠希',38,175.00,1,1,0); --查询
-- 查询所有列
--select * from 表名
select * from students; --一定条件查询(where)
select * from where id=5; -- 查询制定列
select id,name from students; -- 使用as给字段起别名
select id,name as '姓名', age, high, gender from students; -- 通过表名字段查询
select students.name from students; -- 给表起别名查询
select s.id,s.name,s.age from students as s; --消除重复行
-- distinct
select distinct age from students; --条件查询
--比较运算符
-- 查询年纪大于18岁的信息
select * from students where age > 18; --18岁到28岁之间(and)
select * from students where age >= 18 and age =< 28;
select * from students where age between 18 and 28
--在18岁以上或者身高180以上的人(or)
select * from students where age > 18 or high > 180; -- 模糊查询
-- like
-- % 替代1个或者多个甚至是没有
-- 查询姓名中有‘小’的所有名字
select * from students where name like '%小%'; -- 查询两个字人的名字
select * from students where name like '__'; -- 查询至少有2个字的名字
select * from students where name like '%__%'; --范围查询
-- in (1,3,8)表示在一个非连续的范围内
-- 查询 年纪为18和34的人
select * from students where age in (18, 34); --查询 年龄在17岁到34岁之间的信息
select * from students where age between 17 and 34; --查询 年纪不在18到34岁的信息
select * from students where age not between 17 and 34; -- 空判断
-- 判断is null
-- 查询身高为空的信息
select * from students where high is null; -- 判断非空is not null
select * from students where high is not null; -- 排序
-- order by 字段
-- asc从小到大排列,即升序
-- desc从大到小排序,即降序 -- 查询年纪在18到34岁之间的男性,按照年纪从小到大
select * from students where gender=1 and age between 18 and 34 order by age; -- 查询年纪在18到34岁之间的女性,身高从高到矮
select * from students where gender=2 and age between 18 and 34 order by high desc; -- order by 多字段
-- 查询年纪在18到34岁的女性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序
select * from students where age between 18 and 34 and gender=2 order by high desc; -- 查询年纪在18到34岁的男性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序,如果年龄也相等那么按照id从小到大排序;
select * from students where age between 18 and 34 and gender=1 order by high desc, age, id desc; --聚合函数
-- 总数
-- count
-- 查询男性有多少人
select count(*) from students where gender=1; -- 最大值
-- max
-- 查询最大的年纪
select max(age) from students; -- 查询女性的最高 身高
select max(high) from students where gender=2; -- 最小值
-- min
select min(high) from students; -- 求和
-- sum
-- 计算所有人的年龄总和
select sum(age) from students; -- 平均值
-- avg
-- 计算平均年纪
-- 计算平均年纪 sum(age)/count(*)
select sum(age)/count(*) from students;
select avg(age),2 from students;
-- 保留2位小数
select round(avg(age),2) from students; -- 分组
-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by gender; -- 计算每组性别的人数
select gender, count(*) from students group by gender; -- 查询男性组中的姓名 group_concat
select gender,group_concat(name) from students where gender=1 group by gender; -- having
-- 查询每个性别平均年纪超过30岁的性别,以及姓名 having avg(age) > 30
select gender, group_concat(name) from students group by gender having avg(age) > 30; -- 查询每种性别中的人数多于4个的组的信息
select gender,group_concat(name) from students group by gender having count(*)>4;
-- 分页
-- 显示5页
select * from students limit 5; -- 分页显示,每页显示2条数据
select * from students limit 0, 2; -- 按照身高从高到矮排序,查找出所有女性,并且分页显示,每页显示2条数据
select * from students where gender=2 order by high desc limit 0,2;
mariadb 3的更多相关文章
- Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境
首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...
- 续 CentOS7(mini) 运行MVC5 + Mariadb
上一篇,介绍了在CentOS7上使用mono官方二进制安装包快速安装mono环境 并且成功运行了一个Owin自宿主应用(Booker) 由于Owin自宿主应用不需要System.Web的支持,所以可以 ...
- 读书笔记--SQL必知必会--常用MySQL(MariaDB)命令
DBMS信息 显示DBMS的版本 select version(); 显示DBMS状态 status; 显示DBMS资源状态 show status; 显示DBMS支持的权限 show privile ...
- 从MySQL 5.5迁移到Mariadb 10.1.14
从MySQL 5.5迁移到Mariadb 10.1.14 迁移计划如下: 1.备份MySQL 5.5的数据库,对指定库进行备份. 2.还原到Mariadb,然后建立复制. 3.然后就可以愿意啥时候切换 ...
- mariadb数据库忘记密码如何找回
1.systemctl stop mariadb ==>停止mariadb数据库 2.mysqld_safe --skip-grant-tables & ==>进入单机模式 3.m ...
- CentOS 7 x64下Apache+MySQL(Mariadb)+PHP56的安装
每次搭建新服务器,都要来来回回把这些包再装一下,来来回回搞了不下20遍了吧,原来都是凭经验,配置过程中重复入坑是难免的,故写此文做个备忘.虽然有像xampp这样的集成包,但是在生产环境的Linux发行 ...
- Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)
翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚 ...
- CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置
1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB,两条命令都可以 systemctl sta ...
- MySQL/MariaDB/PerconaDB-提权条件竞争漏洞
背景 2016年11月01日,国外安全研究员Dawid Golunski在 MySQl, MariaDB 和 PerconaDB 数据库中发现条件竞争漏洞,该漏洞允许本地用户使用低权限(CREATE/ ...
- CentOS7下安装配置MariaDB
参考: http://www.2cto.com/os/201504/394141.html http://outofmemory.cn/code-snippet/2533/mysql-create-d ...
随机推荐
- 前端面试?这份手撸Promise请你收下
前言 现在很多大厂面试前端都会要求能够手动的写出一个Promise,所以这里整理了一份手写的Promise. 绝对详细,功能绝对强大.如果你不了解Promise的基本使用,那么本篇文章可能不太适合你, ...
- Android 用空格作为分割符切割字符串
项目中有需要用到空格作为分割符切割字符串,进而转为List. String wordStore = edWord.getText().toString(); String[] word = wordS ...
- NRF52840 添加 led service
记录NRF52840 添加LED service的流程,以及遇到的问题. 由于SDK中已经有了led service的.c和.h文件,因此只需要添加文件,并且调用相关函数即可. 注:编译调试环境为ke ...
- SpringBoot+MyBatis整合报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
项目启动的时候报这个错误,这个问题我百度了一天,果然不出意外的还是没能解决,其中有一篇文章相对来说还是有点用的:https://blog.csdn.net/qq8693/article/details ...
- layui上传同一张图片第二次时choose没有反应
将上传文件的input的val设置为空 $("#test11").parent().find("input").val('');
- mysql高级内容学习总结
创建索引 create [unique] index indexname on tablename(columnname(length)) alter tablename add [unique] i ...
- vps的搭建
最近一直想自己搭建一款vps使用,但是苦于一直没有时间,直到今天得空,与大家一起分享下. 服务商的选择 因为自己之前在 vultr 上还留有余额(60$呢,好几百块大洋呢),所以我的服务商就选择 vu ...
- ID3\C4.5\CART
目录 树模型原理 ID3 C4.5 CART 分类树 回归树 树创建 ID3.C4.5 多叉树 CART分类树(二叉) CART回归树 ID3 C4.5 CART 特征选择 信息增益 信息增益比 基尼 ...
- 点击穿透事件-----CSS新属性
面试被问,一脸懵,被提示,还蒙,好丢脸的感觉....赶紧百度了解 .noclick{ pointer-events: none; /* 上层加上这句样式可以实现点击穿透 */ } 就是说重叠在一起的两 ...
- Codeforces1393 题解(A-D)
AC代码 A. Rainbow Dash, Fluttershy and Chess Coloring 可以推导出\(f_1 = 1, f_2 = 2, ..., f_n = f_{n - 2} + ...