1.环境部署:

syntax语法错误

查询基本使用(条件,排序,聚合函数,分组,分页)

--创建学生表

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);

2.查询所有列

select * from 表名

一定条件查询(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;

找出18岁和28岁的人

select * from students where age=18 or age=28;

模糊查询

like

% 替代1个或者多个甚至是没有

查询姓名中有‘小’的所有名字

select * from students where name like '%小%';

查询两个字人的名字 _表示一个字符

select * from students where name like '__';

查询至少有3个字的名字

select * from students where name like '%___';

范围查询

in (1,3,8)表示在一个非连续的范围内,在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 多字段

将students表中数据按照年龄从大到小排列,年龄相同的情况下按照身高从大到小排列

select * from students order by age desc,high desc;

查询年纪在18到34岁的女性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序

select * from students where (age between 18 and 34) and gender=2 order by high desc,age asc; #如果条件多可以用()来区分

查询年纪在18到34岁的男性,身高从高到矮排序,如果身高相同的情况下按照年纪从小到大排序,如果年龄也相等那么按照id从小到大排序;

select * from students where gender=1 and age between 18 and 34 order by high desc,age asc,id asc;

聚合函数

总数

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;

计算所有男人的年龄总和

select sum(age) from students where gender=1;

平均值

avg

计算平均年纪

计算平均年纪 sum(age)/count(*)

select sum(age)/count(*) from students;

select avg(age) from students;

保留2位小数round(,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岁的性别,以及姓名

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;

查询每种性别中的名字,身高,年龄 #查询多个内容之间要加分隔符(自定义),否则输出的字段是连在一起的,容易混淆。

select gender,group_concat(name,'|',high,'|',age) from students group by gender ;

分页

limit m,n m:从m+1行开始展示 n:显示的行数

显示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 select的更多相关文章

  1. centOS7中Mariadb数据库安装与基本管理

    一.Mariadb数据库安装 1. 直接yum源安装 yum -y install mariadb mariadb-serversystemctl start mariadb /启动Mariadb服务 ...

  2. MySQL与MariaDB核心特性比较详细版v1.0(覆盖mysql 8.0/mariadb 10.3,包括优化、功能及维护)

    注:本文严禁任何形式的转载,原文使用word编写,为了大家阅读方便,提供pdf版下载. MySQL与MariaDB主要特性比较详细版v1.0(不含HA).pdf 链接:https://pan.baid ...

  3. MySQL/MariaDB数据库的存储过程

    MySQL/MariaDB数据库的存储过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程概述 1>.存储过程优势 存储过程把经常使用的SQL语句或业务逻辑封装起 ...

  4. MySQL/MariaDB数据库的视图(VIEW)

     MySQL/MariaDB数据库的视图(VIEW) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.视图概述 1>.什么是视图 视图就是一个虚拟的表,保存有实表的查询结果 ...

  5. Centos7安装使用Mysql(mariadb)

    安装 shell> yum install mariadb-server -y 配置 # 修改文件 /etc/my.cnf [mysqld]datadir=/mydata/data/mysqlc ...

  6. SQL中的CASE的用法

    CASE在SQL语句中,很有点类似java等高级编程语言中的switch这样子的多分枝语句,但是有点不同的是,case后面接的是when,另外,when的后续分枝有点类似if后面接else.这个是我的 ...

  7. CentOS 7 Tomcat服务的安装与配置

    3422人阅读  http://blog.51cto.com/13525470/2073657 一.Linux下的Java运行环境 Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由S ...

  8. mysql实现oracle存储过程默认参数

    我们都知道oracle存储过程支持为参数设置默认值,这样即使存储过程升级,原来的调用也可以不受影响.但是mysql不支持,mariadb也没有支持(截止10.4也是如此).但是这一限制会导致升级麻烦重 ...

  9. 最全的ORACLE-SQL笔记

    -- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...

随机推荐

  1. Highcharts基本名词解释

    1.Highcharts基本组成: 2.名词解释 lang 语言文字对象 所有Highcharts文字相关的设置 chart 图表 图表区.图形区和通用图表配置选项 colors 颜色 图表数据列颜色 ...

  2. 微擎后台进行GET提交

    微擎form表单进行GET提交时,要传递 name 分别为 c , a , m , do 的值 例如: <form action="{php echo $this->create ...

  3. shell脚本if语句后面的中括号[]与java的if后面的小括号不同(),实际上[左中括号相当于test命令

    四.shell 中的条件判断命令 test 和 [   test 命令可以处理 shell 脚本中的各类工作.它产生的不是一般的输出,而是可使用的退出状态.test 命令通过接受各种不同的参数,来控制 ...

  4. Java程序员必备的一些流程图

    Java程序员必备的一些流程图 转自https://juejin.im/post/5d214639e51d4550bf1ae8df 前言: 整理了一些Java基础流程图/架构图,做一下笔记,大家一起学 ...

  5. 解决Javaweb中HTTP500的问题

    当我们新建一个Web项目后,运行时可能出现HTTP-500的错误如下图所示  一般是由于路径配置出错 即你电脑上的Tomcat版本与代码本身版本不一致或没有配置路径造成的 解决方法如下 一.鼠标右击你 ...

  6. vue设置全局query参数

    router.beforeEach((to, from, next) => { // 设置全局店铺ID shopid const shopid = from.query.shopid // 如果 ...

  7. 搭建web服务器---Apache服务器

    一.安装Apache 尽管xampp appserv等提供了方便的集成环境,但实际上部署环境还是下载单独安装的Apache,以避免那些安全问题 二.加载php解析模块,并指定模块处理文件的类型 编辑h ...

  8. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. STM32的系统时钟设置SystemClock_Config()探究

    一.首先了解几个硬件名词: stm32有多种时钟源,为HSE.HSI.LSE.LSI.PLL,对于L系统的,还有一个专门的MSI 1.HSE是高速外部时钟,一般8M的晶振,精度比较高,比较稳定. 2. ...

  10. 批处理(.bat)文件使用笔记

    color: 一位参数时→改变字体颜色,例如 color a 就是修改字体为亮绿色. 两位参数时→改变背景和字体颜色,第一位参数为背景颜色值,第二位参数为字体颜色值. color [BF] B:背景颜 ...