MySQL----DQL(查询数据库表中数据)
##DQL:查询表中的记录
1、语法:
select
字段列名
from
表名列表
where
条件列表
group by
分组字段
having 分组之后的条件
order by
排序
limit
分页限定
2、基础查询
1、多个字段的查询
select 字段名1,字段名2,...from 表名;
*注意:
* 如果查询所有字段,则可以使用*来代替字段列表。
2、去除重复
* distinct
3、计算列
* 一般可以使用四则运算计算一些列的值。(一般只会进行数值型的计算)
* ifnull(表达式1,表达式2):null 参与的运算,计算结果都为null
表达式1:哪个字段需要去判断是否为null
表达式2:如果该字段为null后的替换值
4、起别名
*as:as也可以省略
例子:
/*在 lxy数据库中创建一张学生表*/
create table student(
id int,
name varchar(20),
age int,
sex varchar(5),
address varchar(100),
math int,
english int
);
/*插入数据*/
insert into student(id,name,age,sex,address,math,english) values
(1,'马云',66,'男','杭州',100,100);
insert into student(id,name,age,sex,address,math,english) values
(8,'马德',35,'男','香港',78,88);
select * from student ;
/*数据去重*/
select distinct address from student;
/*计算分数之和*/
select name,math,english,math+english from student;
/*如果有Null 参与的运算,计算结果都为Null*/
select name,math,english,math+ifnull(english,0) from student;
/*起别名*/
select name,math,english,math+ifnull(english,0) as Total from student;
3、条件查询
1、where子句后跟条件
2、运算符
* >;<;<=;>=;=;<>(这个是不等于)
* BETWEEN...AND
* IN(集合)
* LIKE
* _:单个占位字符
* %:多个任意字符
* IS NULL
* and 或 &&
* or 或 ||
* not 或 !
例子1:普通查询
/*查询年龄大于20岁*/
select * from student where age >=30;
/*查询年龄不等于30*/
select * from student where age <> 30;
/*查询年龄在30-50之间*/
select * from student where age between 30 and 50;
select * from student where age >= 30 && age <= 50;
select * from student where age >= 30 and age <= 50;
/*查询年龄为45,35,46的信息*/
select * from student where age = 45 or age = 35 or age = 46;
select * from student where age in(45,35,46);
/*查询英语成绩为null*/
/*这样查是不正确的,null不能使用 =(!=)判断*/
select * from student where english = null;
/*正确写法*/
select * from student where english is null;
例子2:模糊查询
/*查询姓马的有哪些*/
select * from student where name like '马%';
/*查询姓马的单名有哪些*/
select * from student where name like '马_';
/*查询第二个字是化的人*/
select * from student where name like '_化%';
/*查询姓名是三个字的人*/
select * from student where name like '___';
/*查询姓名中包含马的*/
select * from student where name like '%马%';
4、排序查询
* 语法:order by 子句
* order by 排序字段1 排序方式1,排序字段2 排序方式2...
* 排序方式:
* ASC:升序,默认的
* DESC:降序
* 注意:
* 如果有多个排序体哦阿健,则当前边的条件值一样时,才会判断第二条件。
/*按照数学成绩排序 降序 */
select * from student order by math desc ;
/*按照数学成绩排序 降序 如果数学成绩一样 按照英语成绩降序排序*/
select * from student order by math desc,english desc ;
5、聚合函数:将一列数据作为整体,进行葱纵向计算
、count:计算个数
1、一般选择非空的列:主键
2、count(*)
2、max:计算最大值
3、min:计算最小值
4、sum:计算和
5、avg:计算平均值
* 注意聚合函数的计算会排除null值。
* 解决方案:
1、选择不包含非空的列进行计算。
2、IFNULL函数
/*计算name的记录条数*/
select count(name) from student;
/* 注意聚合函数的计算会排除null值,解决方案:*/
select count(ifnull(english,0)) from student;
select count(*) from student;
/*计算数学成绩最大值,最小值同理*/
select max(math) from student;
/*求和*/
select sum(math) from student;
/*平均值*/
select avg(math) from student;
6、分组查询
1、语法:group by 分组字段;
2、注意:
1、分组之后查询的字段:分组字段、聚合函数
2、where 和 having 的区别:
1、where在分组之前进行限定,如果不满足条件,则不参与分组。Having在分组之后进行限定,如果不满足结果则不会被查询出来。
2、where后不可以跟聚合函数,having可以进行聚合函数的判断。
/*按照性别分组,分别查询男、女同学的数学平均分*/
select sex, avg(math) from student group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数*/
select sex, avg(math),count(id) from student group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数 要求:分数低于70分的人不参与分组*/
select sex, avg(math),count(id) from student where math > 70 group by student.sex;
/*按照性别分组,分别查询男、女同学的数学平均分,分别的人数 要求:分数低于70分的人不参与分组 分组之后人数大于2个人*/
select sex, avg(math),count(id) from student where math > 70 group by student.sex having count(id) > 2;
select sex, avg(math),count(id) 人数 from student where math > 70 group by student.sex having 人数 > 2;
7、分页查询
1、语法:limit 开始的索引,每页查询的条数;
2、公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
3、limit是一个MySQL的”方言“
/*每页显示3条记录*/
select * from student limit 0,3; /*第一页*/
select * from student limit 3,3; /*第二页*/
MySQL----DQL(查询数据库表中数据)的更多相关文章
- B表中的pid对应A表中id,查询A表中数据,根据b表中对应a表中该id的数据数目排序
B表中的pid对应A表中id,查询A表中数据,根据b表中对应a表中该id的数据数目排序 select a.*,count(*) as c from a left join b on a.id=b.ai ...
- MySQL 两个数据库表中合并数据
两个数据库表中合并数据 如果有 t1 和 t2 两个数据库表格,它们两个对应的字段是相同的.如何将 t2 的数据插入到t1中去呢? insert into t1 select * from t2 ...
- 我们在删除SQL Sever某个数据库表中数据的时候,希望ID重新从1开始,而不是紧跟着最后一个ID开始需要的命令
一.如果数据重要,请先备份数据 二.删除表中数据 SQL: Delete From ('表名') 如:Delete From abcd 三.执行新语句 SQL: dbcc checkident('表 ...
- DQL:data query language用来查询数据库表中的数据
对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 如果没有查询条件,则每次查询所有的行.实际应用中,一般要指定查询的条件.对记录进行过滤. 查询 ...
- sql查询数据库表中重复记录方法
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people where peopleId in (select peopleId ...
- MYSQL:查询单表中不同邮箱最近一次发送状态
1.联系方式表-customer_contact: id email 1 123456@qq.com 2 987643@qq.com 3 hahaha@qq.com 2.发送邮件记录表-contact ...
- [oracle]查询一个表中数据的插入时间
select to_char(scn_to_timestamp(ORA_ROWSCN),'yyyy-mm-dd hh24:mi:ss') insert_time from tablename;
- MySQL数据库中查询数据库表、字段总数量,查询数据总量
最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...
- MySQL查询数据表中数据记录(包括多表查询)
MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...
随机推荐
- Android中Intent的各种常见作用。
Android开发之Intent.Action 1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始. ...
- 牛客网剑指offer第21题——判断出栈序列是否是入栈序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...
- Pom.xml的依赖自动生成
1.第一种用引入jar包的方法 网盘链接:https://pan.baidu.com/s/10HNjNeZc1d5QrFNtvLPWBA 提取码:oako 以上是整个文件直接用idea打开即可 imp ...
- Ubuntu 16.04 apt 国内源
一.推荐几个 Ubuntu 16.04 国内的 apt 源 1. 阿里源 # deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (2 ...
- sql05
1.Ado.net Ado.net是一组由微软提供的使用C#操作数据库的类库 2.连接 首先引入: using System.Data.SqlClient; 需要使用连接字符串进行连接 using S ...
- 分享到微信,QQ等各大网络媒体网站代码
http://www.jiathis.com/ 打开此网站,如果没有账号,请注册一下,然后登陆账号,进入网页以后直接可以复制代码到页面的标签,进行css样式布局,直接可以在页面测试,如果方便的话直接百 ...
- vue之initComputed模块源码说明
要想理解原理就得看源码,最近网上也找了好多vue初始化方法(8个init恶魔...) 因为也是循序渐进的理解,对initComputed计算属性的初始化有几处看得不是很明白,网上也都是含糊其辞的(要想 ...
- 基础JavaScript练习(一)总结
任务目的 在上一任务基础上继续JavaScript的体验 接触一下JavaScript中的高级选择器 学习JavaScript中的数组对象遍历.读写.排序等操作 学习简单的字符串处理操作 任务描述 参 ...
- 获取View的快照
//获取快照 - (UIView*)customSnapshotInView:(UIView*)inview { UIView *snapshot = [inview snapshotViewAfte ...
- node打开本地应用程序
1.打开浏览器 最简单的方法: const cp = require('child_process') cp.exec('start http://127.0.0.1:8889/'); // 自动打开 ...