数据准备
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. NSURLSession http转Https

    1.设置代理 NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defa ...

  2. 【RabbitMQ系列】队列、绑定、交换器

    队列: 从概念上来讲,AMQP消息路由必须有三部分:交换器.队列和绑定.生产者把消息发布到交换器上:消息最终到达队列,并被消费者接收:绑定决定了消息如何从路由器路由到特定的队列. 消费者通过以下两种方 ...

  3. 指令-arContentedit-可编辑的高度自适应的div

    <div  ar-contentedit="true" contenteditable="true"  contenteditable="pla ...

  4. 福州大学W班-助教总结

    开学初对自己的期望 在即将到来的学期前,我希望我可以做到以下几点: 1.多参与同学的课程设计,并提出自己的见解 2.不断提高个人的专业技能,活到老学到老 3.能够及时对同学的博客进行评论,并给出有用的 ...

  5. Vue.js学习

    <!DOCTYPE html> <html> <head> <title>xxx</title> </head> <bod ...

  6. mysql基础篇 - 数据库及表的修改和删除

    基础篇 - 数据库及表的修改和删除         修改和删除 一.实验简介 本节实验中,我们将学习并实践如何对数据库的内容做修改,删除,重命名等操作. 二.实验准备 在正式开始本实验内容之前,需要先 ...

  7. 【iOS】Swift LAZY 修饰符和 LAZY 方法

    延时加载或者说延时初始化是很常用的优化方法,在构建和生成新的对象的时候,内存分配会在运行时耗费不少时间,如果有一些对象的属性和内容非常复杂的话,这个时间更是不可忽略.另外,有些情况下我们并不会立即用到 ...

  8. HNOI 2012 永无乡

    codevs 1477 永无乡 http://codevs.cn/problem/1477/ 2012年湖南湖北省队选拔赛  时间限制: 1 s  空间限制: 128000 KB   题目描述 Des ...

  9. 《javascript设计模式与开发实践》阅读笔记(12)—— 享元模式

    享元模式 享元(flyweight)模式是一种用于性能优化的模式,"fly"在这里是苍蝇的意思,意为蝇量级.享元模式的核心是运用共享技术来有效支持大量细粒度的对象. 享元模式的核心 ...

  10. selenium多个标签页的切换(弹出新页面的切换)

    1_windows = driver.current_window_handle #定位当前页面句柄 all_handles = driver.window_handles #获取全部页面句柄 for ...