SELECT

  select的完整语法:

  1. select col1, col2,... # 业务查询的字段
  2. from table_name # 选取的哪张表
  3. [where single_conditions] # single_conditions条件表达式,个体约束(条件)
  4. [[group by column_name1] # column_name1以哪个字段名分组
  5. [having group_conditions]] # group_conditionds条件表达式,分组约束
  6. [order by column_name2] # column_name2以哪个字段进行排序
  7. [limit N,M] # 执行完之后,跳过N条记录,选取M条记录

  上述如果都有:执行顺序from->where->group by->having->order by->limit->select

  列的结果显示  

    1、去掉重复的数据:distinct(针对于记录而言,不是针对于列的数据而言)

  1. # 查看员工的职位
  2. select title
  3. from s_emp;
  4.  
  5. select distinct title
  6. from s_emp;
  7.  
  8. # 每个部门下有哪些职位
  9. select dept_id,title
  10. from s_emp;
  11.  
  12. select distinct dept_id,title
  13. from s_emp;# 联合唯一

    2、运算符:+、-、*、/、%(只列举一个)

  1. # 查看员工年薪
  2. select salary*12
  3. from s_emp;
  4. select id,last_name,salary*12 as year_salary
  5. from s_emp;
  6.  
  7. # 查看员工当前年薪以及月薪提高100美元后的年薪
  8. select id,last_name,salary*12 old_year_salary,(salary+100)*12 as new_year_salary
  9. from s_emp;

    3、列与列的数据拼接:concat(column_name1, "拼接符", column_name2[, "拼接符"])

  1. # 查看员工基本信息(编号,全名,工资)
  2. select id,concat(first_name,' ',last_name) as name,salary
  3. from s_emp;

    4、将null值转换为特定值:ifnull(column_name, 特定值)

  1. # 查看员工工资(没有工资的显示为0)
  2. select id,last_name,ifnull(salary,0.00) as salary
  3. from s_emp;

  where conditions

    MySQL的运算符概念及作用:

    主要是根据conditions的条件查询结果集

    1、比较运算符:=、<、>、>=、<=、!=、<=>(同is null)、<>(同!=)

  1. # 查看拥有白领工资的员工有哪些(1200,2000)
  2. select id,last_name,salary
  3. from s_emp
  4. where salary>1200 and salary<2000;
  5.  
  6. # 查看有工资的员工信息
  7. select id,last_name,salary
  8. from s_emp
  9. where salary is not null;
  10.  
  11. select id,last_name,salary
  12. from s_emp
  13. where salary <> null;# erro
  14.  
  15. # 查看没有工资的员工的信息
  16. select id,last_name,salary
  17. from s_emp
  18. where salary is null;
  19.  
  20. select id,last_name,salary
  21. from s_emp
  22. where salary <=> null;
  23.  
  24. select id,last_name,salary
  25. from s_emp
  26. where salary != 1200;# null数据未取到

    2、逻辑运算符:and(&&)、or(||)、not

  1. 查看41号部门的员工信息并且工资大于1200或者43号部门工资小于2000的员工信息
  2. select id,last_name,salary
  3. from s_emp
  4. where (dept_id=41 and salary>1200) or (dept_id=43 and salary<2000);

    3、在XXX区间:between ... and ...  不在XXX区间:not between ... and ...

  1. 查看拥有白领工资的员工有哪些(12002000
  2. select id,last_name,salary from s_emp where salary>1200 and salary<2000;
  3. # 下面是闭区间
  4. select id,last_name,salary
  5. from s_emp
  6. where salary between 1199 and 2001; #[1199,2001]
  7.  
  8. # 不在区间内
  9. select id,last_name,salary
  10. from s_emp
  11. where salary not between 1200 and 2000; #(-&,1200)or(2000,+&)

    4、在集合中: in ()    不在集合中:not in ()

  1. # 查看41,42,43号部门的员工有哪些
  2. select id,last_name,salary
  3. from s_emp
  4. where dept_id=41 and dept_id=42 and dept_id=43;
  5. # 和上面的等价
  6. select id,last_name,salary
  7. from s_emp
  8. where dept_id in(41,42,43);
  9.  
  10. # 查看不是41,42,43号部门的员工有哪些
  11. select id,last_name,salary
  12. from s_emp
  13. where dept_id!=41 and dept_id!=42 and dept_id!=43;
  14. # 和上面的等价
  15. select id,last_name,salary
  16. from s_emp
  17. where dept_id not in(41,42,43)

    6、模糊匹配:like :%:0到多个字符匹配;_:1个字符匹配;[ ]:范围内的匹配的单个字符;[^ ]:范围外的匹配的单个字符;   不模糊匹配: not

  1. # 查看职位以VP开头的员工有哪些
  2. select id,last_name,salary
  3. from s_emp
  4. where title like 'VP%';
  5.  
  6. select id,last_name,title
  7. from s_emp
  8. where title not like 'VP%';
  9.  
  10. # 查看员工信息,名字以C开头,并且字符数不小于5个字符
  11. select id,last_name
  12. from s_emp
  13. where last_name like 'C____%';
  14. #查看客户信息,客户名称中包含单引号的客户
  15. select id,name from s_cutomer
  16. where name like "%'%";

  group by column_name

    涉及的组函数:计数count()、最小值min()、最大值max()、平均值avg()、总和sum()

  1. # 查看员工总数
  2. select count(*) as count_num
  3. from s_emp; # 默认分组(以表格为单元)
  4. select count(id) as count_num
  5. from s_emp;
  6.  
  7. # 统计有工资的员工个数
  8. select count(salary) as count_num
  9. from s_emp;
  10. # 查看每个部门的员工个数
  11. select dept_id,count(*) as nums
  12. from s_emp
  13. group by dept_id;
  14.  
  15. # 进行分组后,select的结果只能是组的概念,不允许出现个体概念(last_name)
  16. select dept_id,count(*) as nums,last_name
  17. from s_emp
  18. group by dept_id; # errro
  19.  
  20. # 默认以逗号拼接 group_concat(),这个函数很重要
  21. select dept_id,count(*) as nums,group_concat(last_name)
  22. from s_emp
  23. group by dept_id;
  24.  
  25. # 查看每个部门薪资大于1200的员工总数(信息)
  26. select dept_id,count(*) nums,group_concat(last_name),group_concat(salary)
  27. from s_emp
  28. where salary > 1200
  29. group by dept_id;
  30.  
  31. # 查看部门平均薪资
  32. select avg(salary)
  33. from s_emp
  34. group by dept_id;
  35.  
  36. # 查看部门平均薪资>2000员工总数
  37. select dept_id,count(*) nums,avg(salary)
  38. from s_emp
  39. group by dept_id
  40. having avg(salary)>2000;
  41.  
  42. # 查看每个部门员工总数,部门平均薪资大于1000,并且每个员工的薪资>900
  43. select dept_id,count(*),avg(salary)
  44. from s_emp
  45. where salary >900
  46. group by dept_id
  47. having avg(salary)>1000

    排序order by:升序ASC;逆序DESC

  1. # 查看员工的员工ID,名字,月薪,部门ID,部门ID进行升序排序,相同部门的员工在一起按照薪资从高到低排序
  2.  
  3. select id,last_name,dept_id,salary
  4. from s_emp
  5. order by dept_id asc,salary desc;
  6.  
  7. # 如果进行排序的时候,需要对字段先进行转码,后排序
  8. select id,last_name,dept_id,salary
  9. from s_emp
  10. order by conver(dept_id asc,salary using gbk2312) desc;

    限制记录数目:limit N,M  跳过N条记录,查询M条记录

  1. # 跳过3条记录,查询5条记录,这一般用于分页比较合理
  2. # 擦昏地当前页数和记录数
  3. select id,last_name,dept_id,salary
  4. from s_emp
  5. order by dept_id asc,salary desc
  6. limit 3, 5
  7.  
  8. # 当数据表的记录上万条,比如超过1万条记录
  9. # 建议使用子查询进行分页,这样效率高点,因为子查询是在索引文件(所以你文件比较小,数据文件比较大,处理上就比较慢)上执行的
  10. select id, last_name, dept_id, salary
  11. from s_emp
  12. where id >=(
  13. select id
  14. from s_emp
  15. order by id
  16. limit 10000, 1
  17. )
  18. limit 10;

MySQL查询——select的更多相关文章

  1. mysql 查询select语句汇总

    数据准备: 创建表: create table students( id int unsigned primary key auto_increment not null, name varchar( ...

  2. MySQL的select多表查询

    select 语句: select 语句一般用法为: select 字段名 from tb_name where 条件 ; select 查询语句类型一般分为三种:  单表查询,多表查询,子查询 最简 ...

  3. mysql DML select查询

    windows上的操作 1.从官网下载mysql 下载navicat,用来连接mysql的 2.打开运行启动mysql 3.在navicat上的连接打开新建连接 然后输入链接名,连接名就是用户名,自己 ...

  4. MySQL查询语句(select)详解(1)

    1.查询记录 select*from 表名 [where 条件];eg:select*from students;//查询 students 表中所有记录,所有字段的值都显示出来select fiel ...

  5. mysql数据库(二):查询(SELECT)

    一. 数据库查询—查询(SELECT) 单表查询 多表联合查询 二. 查询—单表查询 查询特定字段: select <字段1,字段2,...> from <表名>; 示例:查询 ...

  6. mysql之select查询:练习

    单表查询: 数据查询命令:select 识别要查询的列 from识别要查询的表 select 运算符: + .-.*./. 加减乘除 等于= 不等于!= 或 <> 大于等于>= 小于 ...

  7. mysql查询性能优化

    mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...

  8. Mysql查询——深入学习

    1.开篇 之前上一篇的随笔基本上是单表的查询,也是mysql查询的一个基本.接下来我们要看看两个表以上的查询如何得到我们想要的结果. 在学习的过程中我们一起进步,成长.有什么写的不对的还望可以指出. ...

  9. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

随机推荐

  1. NCcat学习使用

    一.使用手册 1.简介:nc/NetCat是一款端口监听工具,可以用来建立系统之间的连接.传输文件.TCP代理等. 2.命令参数     nc [-options] hostname port[s] ...

  2. ensorFlow的安装

    WIN7 64位系统 参考了  https://blog.csdn.net/appleyuchi/article/details/71036785 尝试了各种版本,,最后必须python安装3.60版 ...

  3. C#基础表达式语句详解(下)

    书接上文: 4.选择语句:if语句和switch语句 4.1If语句:statement:两种格式:A与B A:If(boolean-expression)embedded-statement(嵌入式 ...

  4. uiautomator 安装和升级

    1.打开uiautomator sdk安装路径 < tools < uiautomatorviewer.bat # 双击点开 2.升级uiautomator包 1.uiautomatorv ...

  5. Git的一些概念(二)

    一.Git的结构 二.Git和代码托管中心 代码托管中心的任务:维护远程库 1. 局域网内 GitLab 服务器 -- 可以自己搭建 2. 外部环境 GitHub 码云 三.本地库和远程库 1. 团队 ...

  6. 流媒体服务器 red5

    https://github.com/Red5/red5-server/releases/tag/v1.0.7-RELEASE   打开此链接  再tar包那里  复制链接地址 Wget此链接地址 W ...

  7. python cookbook3

    1.算GC含量 def validate_base_sequence(base_sequence, RNAflag = False): #判断序列是否只含有A.T.G.C.U seq = base_s ...

  8. Debian 9 部分快捷键失效问题

    教程 具体修复过程: 安装gnome-screensaver包,重启恢复正常.

  9. kali linux2019.4 设置中文字体教程

    最近Kali linux更新到了2019.4版本,下载下来发现找不到设置中文的选项,经过在网上搜索一番发现一个开源的汉语字体包:文泉驿字体 在终端中输入以下命令 apt-get install ttf ...

  10. [原创]浅谈在创业公司对PMF的理解

    [原创]浅谈在创业公司对PMF的理解 在创业时,大多数人都常谈一个词叫"MVP“,但PMF谈的比较少,PMF在创业公司尤为重要,以下谈谈个人一些看法. 1.什么是PMF? 创业公司:一种是找 ...