where条件筛选记录

select id,username,age from uesr where id=5;

alter table user add userDesc varchar(100);
update user set userDesc="This is a test" where id<=5; select id,username,age,userDesc from user where userDesc<=>null; -- <=>检测null
--is [not] null 检测null
select id,username,age,userDesc from user where userDesc is null; --[not] between ... and 选定范围
select id,username,age,sex from user where age between 18 and 20; --[not] in(值...) 指定集合
select id,username,age,sex from user where id in(1,3,5,7,9); --and / or
select id,username,age,sex from user where sex="男" and age>=20;
select id,username,age,sex from user where salary between 20000 and 100000 and sex="男";
select id,username,age,sex from user where id=1 or username="queen"; --[not] like 匹配字符
select id,username,age,sex from user where username like "queen";
--模糊查询 通配符 %任意长度的字符串 _任意一个字符
select id,username,age,sex from user where username like '%三%'; --查询username中有三的用户
select id,username,age,sex from user where username like "___"; --三个_匹配三个字符
select id,username,age,sex from user where username like "张_%"; --查询以张开头的最少两个字符大小
--默认忽略大小写

group by 对记录进行分组

--把值相同放到一个组中,最终查询出的结果只会显示组中一条记录
select id,username,age,sex from user group by sex;
--分组配合group_concat()查看组中某个字段的详细信息
select id,group_concat(username),age,sex from user group by sex;
--显示
+----+----------------------------------------------+-----+-----+
| id | group_concat(username) | age | sex |
+----+----------------------------------------------+-----+-----+
| 1 | king,张三,张子枫,刘德华,吴亦凡,张阿文,经过历 | 23 | 男 |
| 2 | queen,imooc,子怡,王菲 | 27 | 女 |
+----+----------------------------------------------+-----+-----+ --配合聚合函数使用 count() sum() max() min() avg()
select count(*) as total_users from user; --得到总记录数,null也统计,别名total_users
select count(userDesc) from user; --null不统计
--按照sex分组,得到用户名详情,并且分别统计组中的总人数
select group_concat(username) as usersDetail,sex,addr,count(*) as totalUsers from user group by sex;
--显示
+----------------------------------------------+-----+------+------------+
| usersDetail | sex | addr | totalUsers |
+----------------------------------------------+-----+------+------------+
| king,张三,张子枫,刘德华,吴亦凡,张阿文,经过历 | 男 | 上海 | 7 |
| queen,imooc,子怡,王菲 | 女 | 上海 | 4 |
+----------------------------------------------+-----+------+------------+ --按照addr分组,得到用户名的详情,总人数,得到组中年龄的总和,年龄的最大值、最小值、平均值
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers,
sum(age) as ageSum,
min(age) as ageMin,
avg(age) as ageAvg
from user
group by addr;
--显示
+------+-------------------------------+------------+--------+--------+---------+
| addr | usersDetail | totalUsers | ageSum | ageMin | ageAvg |
+------+-------------------------------+------------+--------+--------+---------+
| 上海 | king,queen,张三,张子枫,吴亦凡 | 5 | 161 | 23 | 32.2000 |
| 北京 | imooc,子怡 | 2 | 56 | 25 | 28.0000 |
| 南京 | 刘德华 | 1 | 14 | 14 | 14.0000 |
| 广州 | 王菲 | 1 | 62 | 62 | 62.0000 |
| 湖南 | 经过历 | 1 | 25 | 25 | 25.0000 |
| 西安 | 张阿文 | 1 | 14 | 14 | 14.0000 |
+------+-------------------------------+------------+--------+--------+---------+ --配合with rollup 会在记录末尾添加一条记录,是上面所有记录的总和
select group_concat(username) as usersDetail,
count(*) as totalUsers
from user
group by sex
with rollup; --按照字段位置来分组(addr)
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user
group by 1; --查询age>=30的用户并且按照sex分组
select age,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user
where age>=30
group by sex; --having子句对分组结果进行二次筛选
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user
group by addr
having count(*)>=3;
--having totalUsers>=3; 也可以通过别名的形式二次筛选
--having后面可以加通过聚合函数操作的列(如:sum(),max())或者select查询到的列
--having进行分组条件的指定时,一定要保证分组条件要么为聚合函数,要么条件中的字段必须出现在当前的select语句中

order by 实现排序效果

order by 字段名称 asc|desc  --升序|降序

--测试
select id,username,age
from user
order by id desc;
--order by age; --按照age升序 select id,username,age
from user
order by age asc,id asc; --按照多个字段排序,先age,相同部分id select id,username,age
from user
where age>=30
order by age desc; --有条件的字段排序 select rand(); --产生(0-1)随机数 --实现随机记录
select id,username,age
from user
order by rand(); --每次产生的结果都不同

limit限制结果集的显示条数

limit 值 --显示结果集的前几条记录
limit offset,row_count --从offset开始,显示row_count条记录 --测试
--方式一
select id,username,age,sex from user
limit 5; --方式二
select id,username,age,sex from user
limit 1,5; --offset从0开始,用这种方式实现分页
--显示
+----+----------+-----+-----+
| id | username | age | sex |
+----+----------+-----+-----+
| 2 | queen | 27 | 女 |
| 3 | imooc | 31 | 女 |
| 4 | 张三 | 38 | 男 |
| 5 | 张子枫 | 38 | 男 |
| 6 | 子怡 | 25 | 女 |
+----+----------+-----+-----+ --1.更新user表中的前三条记录,将age加5
update user set age=age+5 limit 3;
--2.将user表中id字段降序排列,更新前三条记录,将age减10
update user set age=age-10 order by id desc limit 3;
--3.删除user表中的前三条记录
delete from user limit 3;
--4.删除user表中id字段降序排列的前三条记录
delete from user order by id desc limit 3;
--update或者delete时limit只支持一个参数形式

单表查询完整select语句的形式

select addr,
group_concat(username) as usersDetail,
count(*) as toTalUsers,
sum(age) as sum_age,
max(age) as max_age,
min(age) as min_age,
avg(age) as avg_age
from user
where id>=2
group by addr
having sum_age>=25
order by sum_age
limit 2;

MySQL基础之数据管理【2】的更多相关文章

  1. MySQL基础之数据管理【4】

    外键约束的使用(只有InnoDB存储引擎支持外键) create table news_cate( id tinyint unsigned auto_increment key comment '编号 ...

  2. MySQL基础之数据管理【3】

    MySQL中的多表联查 --查询emp的id username age depName create table emp( id int unsigned auto_increment key, us ...

  3. MySQL基础之数据管理【5】

    子查询的使用 select 字段名称 from tbl_name where col_name=(select col_name from tbl_name); --内层语句查询的结果可以作为外层语句 ...

  4. MySQL基础之数据管理【1】

    添加记录 insert [into] tbl_name[(col_name,...)] {value|values}(values...); --不指定字段名称时需要按照建表时的字段顺序给每一个字段赋 ...

  5. MySQL基础----py全栈

    目录 MySQL基础----py全栈 一.引言 1.什么是数据? 2.什么是数据库(DB)? 3.什么是数据库管理系统(DBMS)? 4.什么是数据库系统? 5.数据库管理系统由来 6.什么是数据模型 ...

  6. Mysql基础代码(不断完善中)

    Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...

  7. MYSQL基础操作

    MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...

  8. 【夯实Mysql基础】记一次mysql语句的优化过程

    1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同时使 ...

  9. MySQL基础(非常全)

    MySQL基础 一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access ...

随机推荐

  1. js获取屏幕以及元素宽高的方法

    一.window相关 网页正文部分上:window.screenTop 网页正文部分左:window.screenLeft 屏幕分辨率的高:window.screen.height 屏幕分辨率的宽:w ...

  2. 爬虫爬取m3u8视频文件

    一.m3u8视频格式 一般m3u8文件和 视频流ts文件放在同一目录 而m3u8文件格式存放的一般都是ts 文件的一个列表 二.根据m3u8视频存放以及写法的规律 思路 我们一般网站上能找到的m3u8 ...

  3. SimpleDateFormat类简单学习

    一.简介 SimpleDateFormat是一个格式化和解析日期的具体类,其可以将时间转化为指定格式的日期字符串,也可以将具有格式的日期字符串转换为时间. formatting (date → tex ...

  4. Create a Solution using the Wizard 使用向导创建解决方案

    In this lesson, you will learn how to create a new XAF solution. You will also be able to run the ge ...

  5. MySQL基础之练习题

    题目 现有班级.学生以及成绩三张表: 备注:表名称和字段名称可以参考表格内单词设置 根据表格信息,按要求完成下面SQL语句的编写: 1.使用SQL分别创建班级表.学生表以及成绩表的表结构,表内数据可以 ...

  6. MySQL数据库:排序及limit的使用

    排序 排序方式: 升序--asc(默认:从小到大) 降序--desc(由大到小) # 排序语法: order by 字段1 [asc]|desc[,字段2 [adc]|desc,--] limit # ...

  7. 进一步使用 模板缓冲(stencil)

    最近做课题的时候需要计算一个 view(就是一次渲染得到的帧) 下的重叠像素个数(两个物体或更多的物体重叠). 最开始我的想法是渲染一个物体输出一张纹理,这样对比物体之间的纹理就知道重叠了.但是这样当 ...

  8. [配置]VUE中通过process.env判断开发,测试和生产环境,并分环境配置不同的URL HOST

    本文链接:https://blog.csdn.net/tom_wong666/article/details/89763620 Tom哥的博客博文分类和索引页面地址:https://blog.csdn ...

  9. LeetCode 5126. 有序数组中出现次数超过25%的元素 Element Appearing More Than 25% In Sorted Array

    地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/element-appearing-more-than-25-in-so ...

  10. 让你的网页"抖起来"?!?

    细心的小伙伴可能发现我的左下角有一个抖起来的小按钮,然后页面就开始皮了起来,哈哈好快乐啊 没有利用js,单独的使用了css3的动画就实现了这个效果 css设置 @keyframes shake-it{ ...