单表查询语法:

SELECT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数

关键字的执行顺序:

这个是非常重要的,不了解以后会有很多坑。

1.form        找到表:from

2.where        拿着where指定的约束条件,去文件/表中取出一条条记录

3.group by    将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组

4.having        将分组的结果进行having过滤

5.select        执行select

6.distinct        去重

7.order by     将结果按条件排序:order by

8.limit        限制结果的显示条数

准备数据源

建立一个名为student的学生表,下面是他们的字段名及字段类型表。

create table student(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male',
age int(3) unsigned not null default 28,
birthday date not null,
class_id int
);

建表代码

效果图:

insert into student(name,sex,age,birthday,class_id) values
('成龙','male',48,'',1),
('歪歪','female',48,'',2),
('丫丫','female',38,'',2),
('丁丁','female',18,'',2),
('星星','female',18,'',2),
('格格','female',28,'',2),
('张野','male',28,'',3),
('程咬金','male',18,'',3),
('程咬银','female',18,'',3),
('程咬铜','male',18,'',3),
('程咬铁','female',18,'',3);

数据代码

效果图:

普通查询

# 简单查询
select * from student; # 查询表中所有记录
select name, age from student; # 查询表中的name和age字段 # 去重查询
select distinct age from student; # 去重查询表中所有人的年龄 # 查询加四则运算
select name, age-1 from student; # 这样查询出来的为两个字段,字段名分别为name和age-1,age-1字段中值为原始年龄-1
select name, age-1 as 年龄减一 from student; # 查询同上,默认的age-1字段名改成了年龄减一。 # 定义显示格式
# ----------concat()函数用于连接字符串
select concat("姓名:",name," 年龄",age+1)as 显示 from student; # ----------concat_ws()第一个参数为分隔符
select concat_ws("__",name, age) as 显示 from student; # ----------结合case语句
select
(
case
when name="成龙" then
name
when name = "丫丫" then
concat(name,"_美女")
else
concat(name,"_凡人")
end
)as new_name
from student;

where约束查询

where语句中可以使用:

  • 比较运算符:>,<,>=,<=,<>,!=
  • between 80 and 100,值在80到100之间
  • is null,判断某个字段是否为NUll不能用等号,要用is。空字符串并不是null。
  • in(10,20,30),值是10或者20或者30
  • like "张%",模糊查询,找"张"开头的。
    • %:表示任意0个或多个字符。可匹配任意类型和长度的字符
    • _:表示任意单个字符
    • [ ]:表示括号内所列字符中的一个(类似正则表达式)。
    • [^ ]:表示不在括号所列之内的单个字符。
# 单条件查询
select name from student where age>20; # 多条件查询
select name from student where age>20 and sex="male"; # 关键字between .. and ..
select name from student where age between 20 and 40; # 关键字is null
select class_id from student where class_id is null;
select class_id from student where class_id is not null; # 关键字in
select name from student where age in (18, 20, 38);
select name from student where age not in (18, 20, 38); # 模糊查询
select * from student where name like "成%";

group by 分组查询

首先要明确一点,分组是在where之后进行的。

分组是按照某个相同的字段进行归类。比如:班级,性别。

注意:

当我们进行分组之后,只能查询分组字段,想要获取组内其他相关信息,需要借助函数。

# 当我们进行分组之后,只能查询分组字段,想要获取组内其他相关信息,需要借助函数。

# GROUP BY关键字和GROUP_CONCAT()函数一起使用
select class_id, group_concat(name)as 组内名字 from student group by class_id; # GROUP BY与聚合函数一起使用
select class_id, group_concat(name)as 组内名字, count(*)as 组人数 from student group by class_id;

常用的聚合函数:

COUNT(),MAX(),MIN(), AVG(),SUM()

having过滤

#!!!执行优先级从高到低:where > group by > having
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。 #2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数

order by 排序

# 按单列排序
select * from student order by age; # 默认升序
select * from student order by age ASC; # 升序排列
select * from student order by age DESC; # 降序排列 # 按多列排序
先按照age升序排序,如果年纪相同,则按照名字降序排序
select * from student
order by age, name desc;

注:因为ordery by 在分组之后,所以也可以用分组的聚合函数。

limit限制查询的记录数

     select * from student
LIMIT 3; #默认初始位置为0 select * from student
LIMIT 0,5; #从第0开始,即先查询出第一条,然后包含这一条在内往后查5条 select * from student
LIMIT 5,5; #从第5开始,即先查询出第6条,然后包含这一条在内往后查5条

使用正则表达式查询

select * from student regexp "^成";

数据库——MySQL——单表查询的更多相关文章

  1. MySQL数据库之单表查询中关键字的执行顺序

    目录 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 2 执行顺序 3 关键字使用语法 MySQL数据库之单表查询中关键字的执行顺序 1 语法顺序 select distinct from ...

  2. MySQL数据库语法-单表查询练习

    MySQL数据库语法-单表查询练习 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客主要是对聚合函数和分组的练习. 一.数据表和测试数据准备 /* @author :yinz ...

  3. python 3 mysql 单表查询

    python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...

  4. Mysql 单表查询-排序-分页-group by初识

    Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...

  5. Mysql 单表查询 子查询 关联查询

    数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...

  6. Mysql 单表查询where初识

    Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...

  7. mysql数据库之单表查询多表查询

    单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...

  8. mysql数据库之单表查询

    单标查询 单表查询语句 关键字执行的优先级 简单查询 where约束 group by 聚合函数 HAVING过滤 order by 查询排序 LIMIT限制查询的记录数 使用正则表达式查询 单表查询 ...

  9. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

随机推荐

  1. vue-cli 3.x安装配置步骤详细说明

      一.vue-cli 3.x简单介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统:是一个类似于 create-react-app 的可以用例命令行快速配置和生成一个 vue 项 ...

  2. 【坑】自动化测试之Excel表格

    参考一位大神的博客项目架构,把元素和数据都参数化,但是总是被excel表格坑 1.无法下拉 动作列通过下拉列表来控制,点击下拉列表无反应 解决方案:不知道是不是中间动了什么,因为Excel版本的问题, ...

  3. 简单Java程序向实用程序的过度:二进制文件的读写

    File I/O中常见的文件读写: 1.字节流读写文本文件 FileInputStream; FileOutputStream; 2.字符流读写文本文件 FileReader; FileWriter; ...

  4. python dict list tuple

    Dict 创建 somedict = {} somedict = {"key": value} a = dict(one=1, two=2, three=3) c = dict(z ...

  5. OLEDB 简单数据查找定位和错误处理

    在数据库查询中,我们主要使用的SQL语句,但是之前也说过,SQL语句需要经历解释执行的步骤,这样就会拖慢程序的运行速度,针对一些具体的简单查询,比如根据用户ID从用户表中查询用户具体信息,像这样的简单 ...

  6. vue.extend与vue.component的区别和联系

    一味的闷头开发,却对基础概念缺乏理解,是个大坑... 查阅官网后现对自己的理解记录一下,用于日后复习巩固 Vue.extend({}) 简述:使用vue.extend返回一个子类构造函数,也就是预设部 ...

  7. 在MAC上搭建python数据分析开发环境

    最近工作转型到数据开发领域,想在本地搭建一个数据开发环境.自己有三年python开发经验,马上想到使用numpy.scipy.sklearn.pandas搭建一套数据开发环境. ubuntu的环境,百 ...

  8. Android 马甲包制作流程

    一.马甲包的制作流程 1.配置马甲包的applicationId以及应用名称 在app的build.gradle文件中添加马甲包的配置 android { signingConfigs { confi ...

  9. Android 仿iPhone的日期时间选择器

    可选只选择日期,也可以同时选择时间 只选择日期的情况 同时选择日期和时间的情况 关键代码: findViewById(R.id.selectDateButton).setOnClickListener ...

  10. Android 自定义倾斜字体

    public class RotateTextView extends AppCompatTextView { private static final int DEFAULT_DEGREES = 0 ...