mysql-查询例题大全
表名:student
字段信息
年龄age
性别gender:enum(‘男’,‘女’)
身高height:
查询表格样式
主要有两张表,表络的情况如下
比较运算符
>
>=
<
<=
=
!= 或者 <>
》查询年龄大于18岁的信息
select * from students where age > 18
》查询年龄小于18岁的信息
select * from student where age < 18
逻辑运算符
and
or
not
》查询18到20岁之间的学生,全部字段信息
select * from students where age>18 and age<20;
》查询18岁以上的女性
select * from students where age>18 and gender='女';select * from students where age>18 and gender=2;
》查找18岁以上或者身高超过180(包含)以上的
select * from students where age>18 or height>=180;
》查找不是(18岁以上的女性)
select * from students where not(age>18 and gender=2);
》查年龄不是小于或等于18的,要找女的
select * from students where (not age<=18) and gender=2;
模糊查询
方式一
like
%
_方式二
rlike 正则
》查询姓名以’小’开始的名字
select name from students where name like "小%";
》查询姓名中有’小’的所有名字
select name from students where name like "%小%";
》查询名字是两个字组成的
select name from students where name like "__";
》查询有三个字的名字
select name from students where name like "___";
》查询至少有2个字名字的信息
select name from students where name like "__%";
》查询以李开始的姓名
select name from students where name rlike "^李.*";
》查询以‘郭’开头,以‘城’结尾
select name from students where name rlike "^郭.*城$";
范围查询
in 元组
not in 元组
between ... and...
not between ... and ...
》查询年龄为18,34岁的信息
select * from students where age in (18, 34);
select * from students where age=18 or age=34;
》查询年龄不是12,18,34岁的信息
select * from students where age not in (12,18,34);
》查询年龄在18岁到34岁之间的信息
select * from students where age between 18 and 34;
查询结果包含18和34
》查询年龄不在18到34岁之间的信息
select * from students where age not between 18 and 34;
空判断
is null
is not null
》查出身高记录为空的数据
select * from students where height is null;
》 查询身高记录不为空的数据
select * from students where height is not null;
排序
order by 字段 排序排序有
asc,由小到大
desc,由大到小
》查询年龄在18岁到34岁之间的男性,按照年龄从小到大的顺序排
select * from students where (age between 18 and 34) and gender=1 order by age;select * from students where (age between 18 and 34) and gender=1 order by age asc;
》查询年龄在18到34岁之间的女性,身高由高到矮排序
select * from students where (age between 18 and 34) and gender=2 order by desc;
》查询年龄在18到34岁之间的女性,身高由高到矮排序,如果身高也一样的话,按照id由大到小排
select * from students where (age between 18 and 34) and gender=2 order by desc, id desc;
》查询年龄在18到34岁的女性,按照身高由大到小排,如果身高一样就按照年龄由小到大排,如果年龄也一样按照id从大到小排
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc, id desc;
》 按照年龄从小到大,如果年龄一样大,就按照身高从高到矮排
select * from students order by age asc, height desc;
聚合函数
帮我们计算某个值
countmaxminavgsum
》查询男性有多少人,显示出来,并且让表头名称为‘男性人数’
select count(*) as 男性人数 from students where gender = 1;
》查询班里的最大的年龄
select max(age) from students;
》查询女性的最高身高
select max(height) from students where gender=2;
》计算所有人的年龄总和
select sum(age) from students;
》计算班上平均年龄
select sum(age)/count(*) from students;
》计算班上平均年龄,结果保留三位小数
select round(sum(age)/count(*)),3) from students;
round(数字,保留位数)
》计算男性的平均身高,保留2位小数
select round(avg(height),2) from students where gender=1;
round(数据,位数) ===》 round(3.1415,2) -----> 3.14
分组
一般与聚合函数一起使用
group by 字段group_concat(...)having
先分组,再从组里取数据
》显示班里每一个性别的学生有多少个
select gender, count(*) from students group by gender;
》按照性别分组,并显示每一组中的成员名称
group_concat(字段)
select gender, group_concat(name) from students group by gender;
》计算男性的人数
select gender, count(*) from students where gender=1 group by gender;
》将男性分组,并显示组中男性的姓名年龄和id
select gender, group_concat(name,"_",age,"_",id) from students where gender=1 group by gender;
》查询平均年龄超过30岁的性别,以及姓名
select gender, group_concat(name), avg(age) from students group by gender ;
select gender, group_concat(name), avg(age) from students group by gender having avg(age) >30 ;
》 where 与 having的区别
where 是对最终查询的结果进行一次过滤
having 是对分组后的组进行过滤,过滤后再进行其它操作,比如聚合等等。。
所有能用where的地方,都可以使用having来代替。。 而且having支持聚合查询 having avg(age)
分页
限制查询出来的数据个数
limit用法一
limit 数字m
效果, 只出m个数据用法二
limit 起始下标,显示个数
》查询学生表中前五个数据
select * from students limit 5;
》limit带两个参数的例子
》分页的公式
limit(第N页-1)*每页显示个数, 每页显示个数
》查询学生信息按照年龄升序排序,在结果中从第11条数据开始取,取两个出来
select * from students order by age asc limit 10,2;
链接查询
多个表格间的关联查询
》查询学生表与老师表能够对的上号的信息
学生表的cls_id与班级表的id一致的
select * from students inner join classes on students.cls_id = classes.id
select * from 表1 inner join 表2 on 条件
》查询学生表与老师表能够对的上号的信息,字段要求显示学生表全部字段,班级表只显示name字段
select student.*, classes.name from students inner join classes on students.cls_id=classes.id;
》表名太长,表名以别名方式完成上面的查询。 students别名s, classes别名c
from 表名 as 别名
》上述查询中,把班级的名称放在第一列
》查询学生表与老师表能够对的上号的信息,字段要求显示学生姓名和班级名称
select students.name, classes.name from students inner join classes on students.cls_id = classes.id;
》查询能对的上号的学生与班级信息,显示班级表的班级名称,学生表的全部信息。按照班级名称由小到大排序,班级名称一样的时候,就按照学生ID由小到大来排序
》查询学生表并显示学生对应的班级,有班级的显示班级名称,没班级的让它空着
》根据上表的查询结果,把没有班级的学生信息找出来
mysql-查询例题大全相关推荐
- 网上搜集的MySQL查询语句大全集锦(经典珍藏)
原文地址为: 网上搜集的MySQL查询语句大全集锦(经典珍藏) SQL查询语句大全集锦 MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW ...
- MySQL查询语句大全(列举所有常用查询方式)
文章目录 前提条件 简单查询 直接查询 条件查询 模糊查询 算数运算符 逻辑运算符 in与not in运算符 排序查询 高级查询 范围运算 限制查询 嵌套查询 多表连查 内连接 左连接 右连接 聚合函 ...
- Mysql查询语句大全
简单查询 ## 直接查询 语法:select 字段 from 表名: 举例:select name, age from student; 解析:从 student 表中查询 name 与 age ## ...
- mysql查询性别语句_MySQL查询语句简单操作示例
本文实例讲述了MySQL查询语句简单操作.分享给大家供大家参考,具体如下: 查询 -- 创建数据库 create database python_test_1 charset=utf8; -- 使用数 ...
- mysql 查询小数点_MySQL查询小数点位数
怎么查询某个字段中小数有几位? MySQL数据库: 通过下面sql就可以查出来,有2位col*100,有3位col*1000,一次类推: select * from ws_inventory_item ...
- MySQL语句系列--查询语句大全(有示例)
原文网址:MySQL语句系列--查询语句大全(有示例)_IT利刃出鞘的博客-CSDN博客 简介 本文介绍MySQL查询语法(命令)的使用.包括:基本查询.运算条件.排序.分页.分组.联结.组合.子查询 ...
- MySQL数据库select查询命令大全
MySQL数据库select查询命令大全 --数据库操作前的准备 -- 创建数据库 -- create database python_test_1 charset=utf8; -- 使用数据库 -- ...
- mysql的查询语句大全_sql语句(sql数据库查询语句大全)
sql语句 结构化查询语言(StructuredQueryLanguage)缩写为SQL.结构化查询语言是一种数据库查询和编程语言,用于访问数据以及查询,更新和管理关系数据库系统: 程序功能 创建数据 ...
- MySQL语法语句大全
MySQL语法语句大全 一.SQL速成 ; B/ X* Q; t/ W) v" ]结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表 ...
- c3p0 参数 模糊查询_MySQL模糊查询用法大全(正则、通配符、内置函数等)
文章来源:MySQL模糊查询用法大全(正则.通配符.内置函数等) 原文作者:陈哈哈 来源平台:CSDN SELECT * from table where username like '%陈哈哈%' ...
最新文章
- NLP≠NLU,机器学习无法理解人类语言
- Openstack组件部署 — Keystone功能介绍与认证实现流程
- 图解.net程序集赋予强名称
- 计算机里硬盘图标,计算机中在硬盘图标下面有个其他里PPS图标肿么删?
- 【报错笔记】项目使用了JSONArray类,导了import net.sf.json.JSONArray;包报错
- C# XML 添加,修改,删除Xml节点
- php对二维数据进行排序
- div固定大小文字溢出自动缩小_Figma 教程 | 文字工具
- Java Exceptions
- python经典实例-Python机器学习经典实例
- BraftEditor:React使用编辑器编辑的时候,在服务器上删除文字中不存在的图片
- python布局管理_Python入门-图形用户界面-几何布局管理器-grid布局管理器
- MessageDigest详解
- 题源报刊精品阅读-词汇1
- 程序员课外拓展001:EI收录号Accession number中的数字的含义
- 关闭阿里云的短信提醒
- 【C语言】以通讯录为例理解宿舍管理系统,图书管理系统完成C语言期末作业。源代码见文章末尾
- 如何使用kindle
- Oracle常见sql语句练习及答案(经典题目,方便练习)
- 曾李青的五年投资经验总结:早期创业公司的九种死法
热门文章
- vuejs项目性能优化 - 总结篇
- 文本编辑器(资源统计篇)
- linux命令本质,Linux 的命令机制
- asp.net从入门到精通配套课件_MATLAB从入门到算法实践第八期本周六直播
- 福建二级计算机考试12,福建农林大学2016年12月计算机二级考试通知
- 255.0.0.0子网掩码相应的cidr前缀表示法是?_六十四、前缀,后缀,中缀表达式转化求值问题...
- Vue | 实现页面跳转刷新,在Vue页面中调用其他页面的方法
- linux无效内存访问,x86_64 Linux 3.0:无效的内存地址
- openstack 重启mysql_openstack 重启服务命令
- vue生命周期,vue执行顺序图,钩子函数