1. 单表查询

  1. 语法
select distinct 字段 from 库名.表名
where 条件
group by 字段 # 分组
having 筛选 # 过滤
order by 字段 # 排序
limit 限制条件
  1. 关键字的执行优先级
1. from :找到表
2. where:拿着where指定的约束条件,去文件/表中取出一条条记录
3. group by:将取出的一条条记录进行分组,如果没有 group by则整体作为一组
4. having:将分组的结果进行having过滤
5. select:执行select
6. distinct:去重
7. order by:将结果按条件排序
8. limit:限制结果的显示条数
  1. 查询操作
创建一个表:
create table employee(
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,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
); 插入记录:
insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','北京办事处外交大使',7300.33,401,1),
#以下是销售部门
('歪歪','female',48,'20150311','sale',3000.13,402,2),
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),
#以下是运营部门
('张野','male',28,'20160311','operation',10000.13,403,3),
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;
ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
1. distinct 去重:
select distinct post from employee;
distinct 必须写在所有查询字段的前面 2. 四则运算查询:
select name,salary*12 from employee;
除了乘法外,加减乘除都可以 3. 自定义显示格式 concat 用法
select concat ('姓名:',name,'年薪:',salary*12) as Annual_salary from employee;
as + 新字段名,就是起一个别名的意思 concat_ws() # 第一个参数为分隔符来进行字符串拼接
select concat_ws(':',name,salary*12) as Annual_salary from employee; 4. where 约束:
1. 比较运算符:> < >= <= <> !=
select name from employee where post = 'sale';
2. between 10 and 15 # id值在10到15之间
select * from employee where id between 10 and 15;
3. in(1,3,6) # id值是 1,3,6
select * from employee where id in(1,3,6);
4. like 'egon%'
pattern可以是 % 或 _
% 表示任意多个字符:
select * from employee where name like 'wu%';
_ 表示一个字符:
select * from employee where name like 'al_';结果空
select * from employee where name like 'al__';结果alex
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
select * from employee id > 10 and name like 'al%';
select * from employee not id > 10; not 取反 5. group by 分组:
select post,max(salary) from employee group by post;
统计每个岗位的名称及最高工资
select post from employee group by post,id;
分组时可以跟多个条件,那么这么多个条件同时重复才算是一组,group by 后面多条件用逗号分隔
ONLY_FULL_GROUP_BY 模式:
set global sql_mode = 'ONLY_FULL_GROUP_BY';
如果设置了这个模式,那么select后面只能写 group by 后面的分组依据字段和聚合函数统计结果
注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
select post,group_concat(name) from employee group by post;
# 按照岗位分组,并查看组内所有成员名,通过逗号拼接在一起 SELECT post,GROUP_CONCAT(name,':',salary) as emp_members FROM employee GROUP BY post;
# 按照岗位分组,并查看组内所有成员名:薪资,通过逗号拼接在一起 聚合函数:聚合函数聚合的是组的内容,若是没有分组,则默认一组
count() # 统计个数
max()
min()
avg()
sum() 6. having 分组再进行过滤:
select post,max(salary) from employee group by post having max(salary) > 2000;
having 过滤后面的条件可以使用聚合函数,where不行 7. order by 排序:
升序:
select * from employee order by age;
select * from employee order by age asc;
降序:
select * from employee order by age desc;
多条件排序:
select * from employee order by age asc,salary desc;
按照age字段升序,age相同的数据,按照salary降序排列 8. limit 限制查询的记录数:
select * from employee order by salary desc
limit 3; # 默认初始位置为0,从第一条开始顺序取出3条
limit 0,5 # 从位置0(第一条)开始往后查询5条
limit 5,5 # 从位置5(第六条)开始往后查5条 9. 使用正则表达式查询:
select * from employee where name regexp '^ale';
select * from employee where name regexp 'on$';
select * from employee where name regexp 'm{2}';

2. 多表查询

  1. 多表查询

    笛卡尔积: 将两表所有的数据一一对应生成一张大表.

select * from dep,emp; # 将两个表拼一起
select * from dep,emp where dep.id = emp.dep_id; # 找到两表之间对应的关系记录
select * from dep,emp where dep.id = emp.dep_id and dep.name = '技术'; # 筛选部门名称为技术的大表中的记录
select emp.name from dep,emp where dep.id = emp.dep_id and dep.name = '技术'; # 拿到筛选后的记录的员工姓名字段数据
  1. 连表查询

    1. inner join 内连接
    第一步: 连表
    select * from dep inner join emp on dep.id = emp.dep_id;
    第二步: 过滤
    select * from dep inner join emp on dep.id = emp_id where dep.name = '技术';
    第三步: 找到对应字段数据
    select emp.name from dep inner join emp on dep.id =emp.dep_id where dep.name = '技术';
    1. left join 左连接(left join左边的表为主表,主表记录必须全部显示,辅表没办法对应上的,就通过null来补全)
    select * from dep left join emp on dep.id = emp.dep_id;
    1. right join 右连接
    select * from dep right join emp on dep.id = emp.dep_id;
    1. union 全连接
    select * from dep left join emp on dep.id = emp.dep_id
    union
    select * from dep right join emp on dep.id = emp.dep_id;
    1. 子查询: (一个查询结果集作为另一个查询的条件)
    select name from emp where dep_id = (select id from dep where name = '技术');
    1. 子查询是将一个查询语句嵌套在另一个查询语句中.
    2. 内层查询语句的查询结果,可以为外层查询语句提供查询条件.
    3. 子查询中可以包含: in,not in,any,all,exists,not exists等关键字.
    4. 还可以包含比较运算符: = , != , < , > 等.

MySQL数据库~~~~~查询行(文件的内容)的更多相关文章

  1. 提高MySQL数据库查询效率的几个技巧(转载)

    [size=5][color=Red]提高MySQL数据库查询效率的几个技巧(转)[/color][/size]      MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我 ...

  2. MySql数据库导出csv文件命令

    MySql数据库导出csv文件命令: MySql数据库导出csv文件命令: mysql> select first_name,last_name,email from account into ...

  3. MySQL 数据库查询数据,过滤重复数据保留一条数据---(MySQL中的row_number变相实现方法)

    转自: http://www.maomao365.com/?p=10564 摘要: 下文讲述MySQL数据库查询重复数据时,只保留一条数据的方法 实现思路: 在MySQL数据库中没有row_numbe ...

  4. mysql数据库查询pdo的用法

    最早的php对mysql数据库查询是mysql和mysqli方法,后来php的新版本进一步封住了该方法,于是又pdo,抛开php框架,使用pdo查询数据,使用也是相当简便 <?php ini_s ...

  5. MySql数据库恢复(*frm)文件

    mysql数据库恢复(*frm)文件 WorkBench 在使用虚拟服务器时,服务器提供商一般不会像我们使用本地数据库一样:使用导入导出(这样的文件后缀是*.sql).大部分时候提供的是一个文件夹,里 ...

  6. 将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成

    将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成. 代码比较乱,但是方法还是对的,大家又需要的选择看,希望对博友 有帮助,欢迎留言分享! public class s ...

  7. atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js

    atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js 1. 两个方法:: bat vs mysqldump(推荐)  vs   lang  ...

  8. 一个自动备份mysql数据库的bat文件内容

    自动备份mysql数据库,并已当前的日期时间为目录 copy过去, xcopy将近15年没有用dos命令,还是这么亲切 另 本方法是备份数据库文件,不是dump导出,然后再计划任务中使用,我用的是wa ...

  9. MYSQL数据库的日志文件

    日志文件:用来记录MySQL实例对某种条件做出响应时写入的文件.如错误日志文件.二进制日志文件.慢查询日志文件.查询日志文件等. 错误日志 show variables like 'log_error ...

随机推荐

  1. DAO模式多表联查

    student类: package com.myschool.entity; public class student{  private int studentno; //学号  private S ...

  2. HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)

    Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...

  3. BZOJ 3108: [cqoi2013]图的逆变换

    3108: [cqoi2013]图的逆变换 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 627  Solved: 415[Submit][Statu ...

  4. Nginx安装echo模块echo-nginx-module

    https://github.com/openresty/echo-nginx-module 这个模块不包含在 Nginx 源码中,安装方法: 1. 首先下载模块源码:https://github.c ...

  5. 超简单!asp.net core前后端分离项目使用gitlab-ci持续集成到IIS

    现在好多使用gitlab-ci的持续集成的教程,大部分都是发布到linux系统上的,但是目前还是有很大一部分企业使用的都是windows系统使用IIS在部署.NET应用程序.这里写一下如何使用gitl ...

  6. LeetCode刷题总结-树篇(下)

    本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...

  7. linux之寻找男人的帮助,man和info,

    1.在linux下寻求帮助是一个很好的习惯,幸运的是系统提供了帮助的命令man和info,由于linux指令很多,记忆起来简直麻烦,比如以a开头的指令有100条,linux命令算起来得几千条,记忆却是 ...

  8. rabbitmq~消息失败后重试达到 TTL放到死信队列(事务型消息补偿机制)

    这是一个基于消息的分布式事务的一部分,主要通过消息来实现,生产者把消息发到队列后,由消费方去执行剩下的逻辑,而当消费方处理失败后,我们需要进行重试,即为了最现数据的最终一致性,在rabbitmq里,它 ...

  9. 简单实用的git命令

    1.下载项目 先进入目录然后使用jit $ git clone +"url" 2.项目配置 $ composer install 3.上传项目 $ git add . () $ g ...

  10. ASP.NET Aries 高级开发教程:行内编辑事件怎么新增数据到后台(番外篇)

    前提: 今天又网友又提出了一个问题,说行内编辑保存之前,怎么新增一些数据提交到后台? 对方说看了源码,也没找到怎么处理,这里就写文给解答一下. 解答: 于是我看了一眼源码,只能说你没找到地方: 第12 ...