1. 单表查询

语法

select distinct 字段 from 库名.表名

where 条件

group by 字段 # 分组

having 筛选 # 过滤

order by 字段 # 排序

limit 限制条件

关键字的执行优先级

1. from :找到表

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

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

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

5. select:执行select

6. distinct:去重

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

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

查询操作

创建一个表:

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. 多表查询

多表查询

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

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 = '技术'; # 拿到筛选后的记录的员工姓名字段数据

连表查询

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 = '技术';

left join 左连接(left join左边的表为主表,主表记录必须全部显示,辅表没办法对应上的,就通过null来补全)

select * from dep left join emp on dep.id = emp.dep_id;

right join 右连接

select * from dep right join emp on dep.id = emp.dep_id;

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;

子查询: (一个查询结果集作为另一个查询的条件)

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查询行数据_MySQL数据库~~~~~查询行(文件的内容)相关推荐

  1. mysql获取查询策略语句_MySQL数据库查询性能优化策略

    优化查询 使用Explain语句分析查询语句 Explain 用来分析 SELECT 查询语句,开发人员可以通过分析 Explain 结果来优化查询语句. 通过对查询语句的分析,可以了解查询语句的执行 ...

  2. mysql php 缓存机制_mysql数据库查询缓存原理是什么

    mysql数据库查询缓存原理是:1.缓存SELECT操作的结果集和SQL语句,key为sql,value为查询结果集:2.如果新的SELECT语句到了,以这个sql为key去缓存中查询,如果匹配,就把 ...

  3. mysql 条件查询 导出数据_mysql按查询条件导出指定数据方法

    本文章介绍了用mysql into outfile命令来导入指定表中指定数据的方法. 按条件导出mysql表的数据: 代码如下 theyestoday=`date -d "-1 day&qu ...

  4. mysql自动同步数据_MySQL数据库实现双向自动同步

    [IT168 技术]本文将探讨如何通过MySQL数据库的高级特性,实现数据库的双向自动同步,确保数据的冗余与完整性.通过以往真实的项目实战与经验,把操作实施过程全部记录下来,主要有以下几个主要内容. ...

  5. mysql查询 伪列_Mysql数据库查询到的数据设置伪列显示

    本次查询表为:  tasks 查询的字段为: id,name 查询语句为: select id,name from tasks; 查询结果为: +----+------------+ | id | n ...

  6. mysql sql查询json数据_mysql如何查询json的值

    mysql查询json的值的方法:首先打开命令窗口:然后执行SQL语句"SELECT REPLACE(json_extract(push_data,'$.carRenewalInfoVo.l ...

  7. Mysql查询汉字语法_Mysql数据库查询语法详解

    ___聚合函数___max():最大值min():最小值avg():平均值sum():和count():记数 group_concat():组内字段拼接,用来查看组内其他字段 ___example__ ...

  8. mysql 时间戳查询当天数据_mysql 时间戳查询 当天 本周 当月 数据

    from_unixtime(time_stamp)   ->  将时间戳转换为日期 unix_timestamp(date)             ->  将指定的日期或者日期字符串转换 ...

  9. mysql scrapy 重复数据_mysql数据库如何处理重复数据?

    前言 前段时间,很多人问我能不能写一些数据库的文章,正好自己在测试mysql数据库性能的时候,出现了一个问题,也就是出现了很多重复的数据,想起来自己long long ago写过一篇类似的,仅此就拿来 ...

  10. mysql 删除时间范围数据_mysql数据库按时间删除数据总结

    今天访问程序时报如下异常: 主要原因如下: 数据库所在磁盘空间不够所致. 于是登录数据库所在的服务器,查看了下数据库所占的磁盘空间如下: 可以看到磁盘的33G,被全部占满了,于是就开始了数据库表的清理 ...

最新文章

  1. 个人建议:VSCode和WebStorm中的“关闭其他所有编辑器、关闭左侧编辑器、关闭右侧编辑器”快捷键这么设置shift+alt+w、shift+alt+[、shift+alt+],你会受益匪浅
  2. ADO与ADO.NET的区别与介绍
  3. 【NOIP2013模拟】粉刷匠 题解代码
  4. 已知数据信息为 16位,最少应附加( )位校验位,以实现海明码纠错。
  5. python绘制动画示例_Python使用matplotlib绘制动画的方法
  6. 算法(20)-leetcode-剑指offer4
  7. Apache Kylin从入门到精通
  8. linux 安装 交换分区大小,给已安装的Linux新增Swap交换分区
  9. Python 打印嵌套list中每个数据(遍历列表)
  10. nginx的upstream实现负载均衡自带坏点自动剔除功能
  11. linux调度器(八)——实时调度与SMP
  12. Xshell 6安装和使用教程
  13. Oracle 临时表空间 SQL语句
  14. 阿里云推送证书验证失败
  15. php图书馆占座系统代码,PHP图书馆管理系统(源码+数据库脚本+截图)
  16. asuswrt 单臂路由_Padavan(老毛子) 最简单臂路由组网 VLAN 设置
  17. 学习笔记 第八周 第二篇(修改版)
  18. html首页随机飘浮图片,jQuery 全屏随机漂浮图片广告
  19. Android系统体系结构
  20. api 二次 开发 禅道_禅道 Rest API 开发

热门文章

  1. 一个oracle并发性问题的分析和解决
  2. zabbix通过skype发送报警消息之切换平台
  3. IT草根的江湖之路之七: 挑战,刚刚开始
  4. throw er; // Unhandled 'error' event 和Error: listen EADDRNOTAVAIL 192.168.0.109:8081
  5. 常见错误——给定编码中的字符无效。(xml)
  6. 判断DataTable为空,获取值,查询数据,datarow转datatable
  7. TP 打开 显示错误信息
  8. 文本框换行_word的段落标记与换行,你真的知道么?
  9. 六石编程学:由库调用没测试到,谈工作粗糙
  10. 缺少链接库报错:ld: symbol(s) not found for architecture x86_64