数据准备
drop table if exists class;
create table class(
    class_no int(2) unsigned zerofill primary key auto_increment comment '班级编号',
    class_name varchar(30) not null comment '班级名称'
);
insert into class values(1, '培优班');
insert into class values(2, '普通班');

drop table if exists student;
create table student(
    stu_no int(2) unsigned zerofill primary key auto_increment comment '学员编号',
    stu_name varchar(30) not null comment '学员姓名',
    stu_sex varchar(3) not null comment '学员性别',
    stu_age tinyint(2) unsigned zerofill comment '学员年代',
    class_no int(2) unsigned zerofill comment '所在班级编号',
    foreign key(class_no) references class(class_no)  
);
insert into student values(01, '李白', '男', 18, 01);
insert into student values(02, '杜甫', '男', 20, 01);
insert into student values(03, '张飞', '男', 32, 02);
insert into student values(04, '韩信', '男', 26, 02);
insert into student values(05, '了龙', '男', 27, 02);
insert into student values(06, '大乔', '女', 17, 01);
insert into student values(07, '小乔', '女', 16, 01);
insert into student values(08, '小乔', '女', 16, 01);
insert into student values(09, '关哥', '男', 32, 02);
insert into student values(10, '刘备', '男', 36, null);
alter table student drop foreign key `student_ibfk_1`;

1: [ order by ] 排序
    例: select * from student;
    例: select * from student order by stu_age;        --对学生年龄进行排序
    例: select * from student order by stu_age desc;   --desc表示的就是从大到小排序,默认是升序
    例: select * from student order by stu_age asc;    --asc表示从小到排序,默认的就是asc
    例: select * from student order by stu_age asc, stu_no desc;  --如果一个字段不能区分大小时,则可以按照第二个字段来区分

字符集与校对规则
    例: show character set;   --查看mysql数据库所支持的字符集
    例: show collation;
    例: show collation like 'gbk%';

    ps: 每个字符集都会提供一个或者多个校对规则, 一般的校对规则命名是: 字符集_地域名_ci|cs|bin;
    ci: 表示不区分大小写;
    cs: 表示区分大小写;
    bin: 表示使用编码比较;   [a-z]:[97-122]    [A-Z]:[65-90]

    create table t_1(
        name varchar(20)
    ) character set gbk collate gbk_chinese_ci;
    insert into t_1 values('C'),('a'),('B');
    select * from t_1 order by name;
    
    create table t_2(
        name varchar(20)
    ) character set gbk collate gbk_bin;
    insert into t_2 values('C'),('a'),('B');
    select * from t_2 order by name;
*******************************************************************************************************

2: [ limit ]限制获得的记录数
    语法: limit offset, row count。  offset表示索引值(从0开始), row count表示要获取的记录数
    select * from student;
    例: select * from student limit 1, 5;   --从第二条数据开始获取,获取5条数据
    例: select * from student limit 1, 50;  --如果要获取的数据,超过了总共的记录数,这时则会获取所有的
    例: select * from student limit 5;      --如果只有一个参数时同则表示要获取的记录数,从0开始获取
*******************************************************************************************************

3: [ distinct ] 除去重复的记录
    ps: 只有要查询的字段值全部完全相同时,才能算是重复,并不是部分的字段相同
    例: select distinct stu_name from student;
    例: select distinct stu_name, stu_age from student;
    例: select distinct stu_no, stu_name, stu_age from student;   --这里不能去重,因为没有重复的记录

    例: select all stu_name, stu_age from student;  --all与distinct相反, all表示获取到所有的记录, 但默认就是all

Mysql单表查询(胖胖老师)的更多相关文章

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

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

  2. python 3 mysql 单表查询

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

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

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

  4. Mysql 单表查询where初识

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

  5. MySQL单表查询

    MySQL之单表查询 创建表 # 创建表 mysql> create table company.employee5( id int primary key AUTO_INCREMENT not ...

  6. python mysql 单表查询 多表查询

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  7. mysql 单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数   二 ...

  8. SQL学习笔记四(补充-1)之MySQL单表查询

    阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...

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

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

随机推荐

  1. PHP微信公众平台oauth2.0网页授权登录类的封装demo

    一.微信授权使用的是OAuth2.0授权的方式.主要有以下简略步骤: 第一步:用户同意授权,获取code 第二步:通过code换取网页授权access_token 第三步:拉取用户信息(需scope为 ...

  2. 敏捷冲刺(Beta版本)

    评分基准: 按时交 - 有分(计划安排-10分,敏捷冲刺-70分),检查的项目包括后文的三个个方面 冲刺计划安排(单独1篇博客,基本分5分,根据完成质量加分,原则上不超过满分10分) 七天的敏捷冲刺( ...

  3. Python实现栈

    栈的操作 Stack() 创建一个新的空栈 push(item) 添加一个新的元素item到栈顶 pop() 弹出栈顶元素 peek() 返回栈顶元素 is_empty() 判断栈是否为空 size( ...

  4. 浅谈CPU三级缓存和缓存命中率

    CPU: CPU缓存(Cache Memory)是位于CPU与内存之间的临时存储器,它的容量比内存小的多但是交换速度却比内存要快得多.缓存的出现主要是 为了解决CPU运算速度与内存读写速度不匹配的矛盾 ...

  5. JAVA_SE基础——23.类的定义

    黑马程序员入学blog ... java 面向对象的语言 对象:真实存在的唯一的实物. 比如:我家的狗, 类: 实际就是对某种类型事物的共性属性与行为的抽取.  抽象的概念...   比如说:车   ...

  6. Mybatis框架入门

    Mybaits框架 一.什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了googl ...

  7. 关于 Form 表单的 enctype 属性

    enctype 属性一共有3个值 application/x-www-form-urlencoded 在发送前编码所有字符(默认) multipart/form-data 上传二进制数据, 所以在使用 ...

  8. 从PRISM开始学WPF(九)交互(完结)

    0x07交互 Notification xaml: <Window x:Class="UsingPopupWindowAction.Views.MainWindow" xml ...

  9. Python内置函数(45)——ascii

    英文文档: ascii(object) As repr(), return a string containing a printable representation of an object, b ...

  10. SpringBoot+Angular2 开发环境搭建

    https://segmentfault.com/a/1190000007921675