美文网首页
mysql基本操作crud之(查询)

mysql基本操作crud之(查询)

作者: 流光汐舞 | 来源:发表于2017-09-16 11:28 被阅读0次

查询的基本语法:

select * from 表名;

1)from关键字后面写表名,表示数据来源于是这张表
2)select后面写表中的列名,如果是*表示在结果中显示表中所有列
3)在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中
4)如果要查询多个列,之间使用逗号分隔
5)消除重复行,在select后面列前使用distinct可以消除重复的行

1、条件

使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
语法如下:

select * from 表名 where 条件;

1)比较运算符
等于=
大于>
大于等于>=
小于<
小于等于<=
不等于!=或<>

select * from students where id>3;
select * from students where name!='Tom';
select * from students where isdelete=0;

2)逻辑运算符
and
or
not

select * from students where id>3 and gender=0;
select * from students where id<4 or isdelete=0;

3)模糊查询
like
%表示任意多个任意字符
_表示一个任意字符

select * from students where name like 'T%';
select * from students where name like 'To_';
select * from students where name like 'T%' or name like '%o%';

4)范围查询
in表示在一个非连续的范围内
between ... and ...表示在一个连续的范围内

select * from students where id in(1,3,8);
select * from students where id between 3 and 8;
select * from students where id between 3 and 8 and gender=1;

5)空判断
注意:null与''是不同的
判空is null
判非空is not null

select * from students where hometown is null;
select * from students where hometown is not null;
select * from students where hometown is not null and gender=0;

6)优先级
小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用

2、聚合

为了快速得到统计数据,提供了5个聚合函数。
count(*):计算总行数,括号中写星与列名,结果是相同的。
max(列):求此列的最大值。
min(列):求此列的最小值。
sum(列):求此列的和。
avg(列):求此列的平均值。

select count(*) from students;
select max(id) from students where gender=0;
select min(id) from students where isdelete=0;
select sum(id) from students where gender=1;
select avg(id) from students where isdelete=0 and gender=0;

3、分组

select
from
where 分组之前的过滤
group by
having 分组之后的过滤
limit
1)按照字段分组,表示此字段相同的数据会被放到一个组中
2)分组后,只能查询被分组的列和聚合函数
3)可以对分组后的数据进行统计,做聚合运算

select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
如:
select gender as 性别,count(*)
from students
group by gender;

having后面的条件运算符与where的相同

select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
如:
select gender as 性别,count(*)
from students
group by gender
having gender=1;

where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
having是对group by的结果进行筛选

4、排序

将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
默认按照列值从小到大排列
asc从小到大排列,即升序
desc从大到小排序,即降序

select * from 表名
order by 列1 asc|desc,列2 asc|desc,...
如:
select * from students
where gender=1 and isdelete=0
order by id desc;

5、分页

从start开始,获取count条数据
start索引从0开始

select * from 表名
limit start,count

已知:每页显示m条数据,当前显示第n页
求总页数:此段逻辑后面会在python中实现
查询总条数p1
使用p1除以m得到p2
如果整除则p2为总数页
如果不整除则p2+1为总页数
求第n页的数据。

select * from students
where isdelete=0
limit (n-1)*m,m

总结

完整的select语句

select 
from 表名
where ....
group by ... 
having ...
order by ...
limit star,count

执行顺序为:

from 表名
where ....
group by ...
having ...
order by ...
limit star,count

相关文章

  • mysql基本操作crud之(查询)

    查询的基本语法: 1)from关键字后面写表名,表示数据来源于是这张表2)select后面写表中的列名,如果是*表...

  • Mysql学习之二 Mysql概念及安装

    一、程序员的数据库要求 基本的SQL操作、CRUD操作 多表连接查询、分组查询和子查询。 常用数据库的的单行函数。...

  • mysql基本操作crud之(增删改)

    1、数据库操作 创建数据库 删除数据库 切换数据库 查看当前选择的数据库 2、表操作 查看当前数据库中所有表 创建...

  • MySQL的基本语法

    MySQL的基本语法 CRUD的操作: C:create 增 R:read 查 U:update 修改 D:de...

  • golang第五天

    学习go操作mysql,crud测试 代码 总结 go操作mysql 打卡时间: 21:04

  • 项目知识点汇总

    目标 基本操作( crud) 组件使用 组件交互 axios 1、查询--form的使用- 主要用于列表 :dat...

  • python批量操作mysql

    工具 mysql基本操作 #mysql没有top的用法,查询前N条需要用limit python批量操作mysql...

  • 目录

    1.使用JPA实现MySQL数据库基本CRUD操作(https://www.jianshu.com/p/fd988...

  • mongoose学习笔记

    首先是基础crud操作 查询简单查询条件查询 插入 更新 查询 简单查询 mongoose: 条件查询 (>) 大...

  • 一篇文章,总结MySQL基本操作

    MySQL的基本操作可以包括两个方面:MySQL常用语句如高频率使用的增删改查(CRUD)语句和MySQL高级功能...

网友评论

      本文标题:mysql基本操作crud之(查询)

      本文链接:https://www.haomeiwen.com/subject/jzuysxtx.html