(七)MySQL数据操作DQL:单表查询1
(1)单表查询
1)环境准备
mysql> CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);
insert into
company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('tianyun','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);
2)简单查询
语法
select 字段1,字段2 from 表名;
select * from 表名;
查询表中指定字段的数据
mysql> select name,salary,post from employee5;
+-----------+----------+------------+
| name | salary | post |
+-----------+----------+------------+
| jack | 5000.00 | instructor |
| tom | 5500.00 | instructor |
| robin | 8000.00 | instructor |
| alice | 7200.00 | instructor |
| tianyun | 600.00 | hr |
| harry | 6000.00 | hr |
| emma | 20000.00 | sale |
| christine | 2200.00 | sale |
| zhuzhu | 2200.00 | sale |
| gougou | 2200.00 | sale |
+-----------+----------+------------+
查询表中的所有数据
mysql> select * from employee5;
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 |
| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 |
| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 |
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
| 5 | tianyun | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 |
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
3)避免去重distinct,通常仅用于某一个字段
语法
select distinct 字段 from 表名;
查询雇员表中的部门,把部门字段去重
mysql> select distinct post from employee5;
+------------+
| post |
+------------+
| instructor |
| hr |
| sale |
+------------+
3 rows in set (0.00 sec)
mysql> select post from employee5;
+------------+
| post |
+------------+
| instructor |
| instructor |
| instructor |
| instructor |
| hr |
| hr |
| sale |
| sale |
| sale |
| sale |
+------------+
10 rows in set (0.00 sec)
4)通过四则运算查询,
语法:
select 字段1,字段2,字段数字 from 表名;
select 字段1,字段2,字段2数字 as 新字段名字 from 表名
select 字段1,字段2,字段2*数字 新字段名字 from 表名
根据月薪水查14年薪
mysql> select name,salary,salary*14 from employee5; \\创建一个新的年薪14薪字段(salary*14)
mysql> select name,salary,salary*14 as annual_salary from employee5; \\给salary*14这个设置一个别名
mysql> select name,salary,salary*14 annual_salary from employee5; \\salary*14 设定别名(annual_salary)
mysql> select name,salary,salary*14 from employee5;
+-----------+----------+-----------+
| name | salary | salary*14 |
+-----------+----------+-----------+
| jack | 5000.00 | 70000.00 |
| tom | 5500.00 | 77000.00 |
| robin | 8000.00 | 112000.00 |
| alice | 7200.00 | 100800.00 |
| tianyun | 600.00 | 8400.00 |
| harry | 6000.00 | 84000.00 |
| emma | 20000.00 | 280000.00 |
| christine | 2200.00 | 30800.00 |
| zhuzhu | 2200.00 | 30800.00 |
| gougou | 2200.00 | 30800.00 |
+-----------+----------+-----------+
10 rows in set (0.00 sec)
mysql> select name,salary,salary*14 as annual_salary from employee5;
+-----------+----------+---------------+
| name | salary | annual_salary |
+-----------+----------+---------------+
| jack | 5000.00 | 70000.00 |
| tom | 5500.00 | 77000.00 |
| robin | 8000.00 | 112000.00 |
| alice | 7200.00 | 100800.00 |
| tianyun | 600.00 | 8400.00 |
| harry | 6000.00 | 84000.00 |
| emma | 20000.00 | 280000.00 |
| christine | 2200.00 | 30800.00 |
| zhuzhu | 2200.00 | 30800.00 |
| gougou | 2200.00 | 30800.00 |
+-----------+----------+---------------+
10 rows in set (0.00 sec)
mysql> select name,salary,salary*14 annual_salary from employee5;
+-----------+----------+---------------+
| name | salary | annual_salary |
+-----------+----------+---------------+
| jack | 5000.00 | 70000.00 |
| tom | 5500.00 | 77000.00 |
| robin | 8000.00 | 112000.00 |
| alice | 7200.00 | 100800.00 |
| tianyun | 600.00 | 8400.00 |
| harry | 6000.00 | 84000.00 |
| emma | 20000.00 | 280000.00 |
| christine | 2200.00 | 30800.00 |
| zhuzhu | 2200.00 | 30800.00 |
| gougou | 2200.00 | 30800.00 |
+-----------+----------+---------------+
10 rows in set (0.00 sec)
5)定义显示格式:contcat()函数用于连接字符串
mysql> select concat(name,'annual salary:',salary*14) as annual_salary from employee5;
+---------------------------------+
| annual_salary |
+---------------------------------+
| jackannual salary:70000.00 |
| tomannual salary:77000.00 |
| robinannual salary:112000.00 |
| aliceannual salary:100800.00 |
| tianyunannual salary:8400.00 |
| harryannual salary:84000.00 |
| emmaannual salary:280000.00 |
| christineannual salary:30800.00 |
| zhuzhuannual salary:30800.00 |
| gougouannual salary:30800.00 |
+---------------------------------+
(2)单条件查询
语法:
select 字段1,字段2 from 表名 where 条件;
1)单条件查询
mysql> select name,salary from employee5 where name='jack';
+------+---------+
| name | salary |
+------+---------+
| jack | 5000.00 |
+------+---------+
2)多条件查询:查询工资大于5000和小于20000的姓名
语法:
select select 字段1,字段2 from 表名 where 条件1 and 条件2;
select select 字段1,字段2 from 表名 where 条件1 or 条件2;
mysql> select name,salary from employee5 where salary > 5000 and salary <20000;
+-------+---------+
| name | salary |
+-------+---------+
| tom | 5500.00 |
| robin | 8000.00 |
| alice | 7200.00 |
| harry | 6000.00 |
+-------+---------+
3)关键字between and
查询工资大于等于5000和小于等于20000的姓名
mysql> select name,salary from employee5 where salary between 5000 and 20000;
mysql> select name,salary from employee5 where salary not between 5000 and 20000;
4)关键字 is null
查询字段是空的值
mysql> select name,job_description from employee5 where job_description is null;
5)关键字in 集合查询
查询工资是5000和6000的都是哪些人,和不是哪些人
mysql> select name,salary from employee5 where salary=5000 or salary=6000;
mysql> select name,salary from employee5 where salary in(5000,6000);
mysql> select name,salary from employee5 where salary not in(5000,6000);
6)模糊查询,关键字:like
通配符:%,匹配任意长度字符
mysql> select * from employee5 where name like 'al%';
通配符:_ 匹配任意单个字符
mysql> select * from employee5 where name like 'al_';
(3)查询排序
1)单列排序
按照薪水排序
mysql> select * from employee5 order by salary; //升序
按照薪水降序
mysql> select * from employee5 order by salary desc; //降序
2)按多列排序
先按入职时间排序,再按薪水排序
mysql> select * from employee5 order by hire_date ,salary asc;
(4)限制查询的记录数
mysql> select * from employee5 order by salary limit 5; \\取前5条数据,默认初始位置是0
mysql> select * from employee5 order by salary limit 4,5; \\从第3条数据开始,取5条数据
(5)使用集合函数查询
count(*) 统计行数据
max(字段) 取字段最大值
min(字段) :取字段最小值
avg(字段) :平均
sum(字段) :取字段和
mysql> select count(*) from employee5;
mysql> select count(*) from employee5 where post='hr';
查询薪水最高的人所有信息
mysql> select * from employee5 where salary = (select max(salary) from employee5); //子查询
(6)分组查询
1)group by和group_concat()函数一起使用
根据部门id进行分组,把相同部门的人统计出来(拼接)
mysql> select dep_id,group_concat(name) from employee5 group by dep_id;
根据部门进行分组,把部门的每个人姓名打印出来(拼接)
mysql> select post,group_concat(name) from employee5 group by post;
2)group by和集合函数一起使用
根据部门进行分组,取每个部门薪水最大的
mysql> select post,max(salary) from employee5 group by post;
(7)使用正则进行匹配
mysql> select name,salary from employee5 where name regexp 'm{2}';
mysql> select name,salary from employee5 where name regexp 'yun$';
mysql> select name,salary from employee5 where name regexp '^m';
(七)MySQL数据操作DQL:单表查询1的更多相关文章
- (七)MySQL数据操作DQL:多表查询2
(1)准备环境 1)创建员工表 mysql> create table company.employee6( -> emp_id int auto_increment primary ke ...
- mysql第四篇:数据操作之单表查询
单表查询 一.简单查询 -- 创建表 DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCRE ...
- 06-查询操作(DQL)-单表查询
一. 综述 查询操作主要从两个方面来说:单表查询和多表查询. 单表查询包括:简单查询.过滤查询.结果排序.分页查询.聚集函数. 二 . 案例设计 1. 设计产品表(product).包括:主键 ...
- mysql第四篇:数据操作之多表查询
mysql第四篇:数据操作之多表查询 一.多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment ...
- Mysql基础(四):库、表、记录的详细操作、单表查询
目录 数据库03 /库.表.记录的详细操作.单表查询 1. 库的详细操作 3. 表的详细操作 4. 行(记录)的详细操作 5. 单表查询 数据库03 /库.表.记录的详细操作.单表查询 1. 库的详细 ...
- mysql查询操作之单表查询、多表查询、子查询
一.单表查询 单表查询的完整语法: .完整语法(语法级别关键字的排列顺序如下) select distinct 字段1,字段2,字段3,... from 库名.表名 where 约束条件 group ...
- 查询数据SELECT 之单表查询
一.单表查询的语法与关键字的执行优先级""" # 单表查询# 单标查询完整与法:# select distinct(关键字,代表查询的意思,后面跟)字段1,字段2...( ...
- mysql四-1:单表查询
一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二.关键 ...
- 07-查询操作(DQL)-多表查询
一. 综述 查询操作主要从两个方面来说:单表查询和多表查询. 多表查询包括:笛卡尔积.外键约束.内连接查询.外链接查询.自连接查询. 二 . 案例设计 1. 设计产品表(product). ...
随机推荐
- php array_merge和“+”的区别和使用《细说php2》
php array_merge和“+”的区别和使用
- JavaScript页面跳转
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...
- 【bzoj4516】[Sdoi2016]生成魔咒 后缀数组+倍增RMQ+STL-set
题目描述 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2].一个魔咒串 S 的非空字串被称为魔咒串 S 的生成魔咒. 例如 S=[1,2 ...
- P1650 田忌赛马
题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币. ...
- hihocoder 1465 循环串匹配问题(后缀自动机)
后缀自动机感觉好万能 tries图和ac自动机能做的,后缀自动机很多也都可以做 这里的循环匹配则是后缀自动机能做的另一个神奇功能 循环匹配意思就是S是abba, T是abb 问'abb', 'bba' ...
- 【题解】HNOI2008GT考试
这题好难啊……完全不懂矩阵加速递推的我TAT 这道题目要求我们求出不含不吉利数字的字符串总数,那么我们有dp方程 : dp[i][j](长度为 i 的字符串,最长与不吉利数字前缀相同的后缀长度为 j ...
- [CF327E]Axis Walking([洛谷P2396]yyy loves Maths VII)
题目大意:给一个长度为$n(1\leqslant n\leqslant24)$的序列$S$和$k(0\leqslant k\leqslant2)$个数. 求有多少种$S$的排列方式使得其任何一个前缀和 ...
- [AT2558]Many Moves
题目大意:有$n$个位置$1,2,\dots n$:你有两个棋子$A$和$B$,你要进行$q$次操作,第$i$次操作给定一个$x_i$,你要选择一个棋子移动到$x_i$:求两个棋子最小移动的步数之和. ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Android-使用ViewFlipper实现轮番切换广告栏
所谓的轮番切换广告栏,指的是下面这个东西,笔主不知道该怎么确切描述这货... 笔主没有百度研究过其他大牛是怎么实现这个功能的,在这里笔主充分发挥DIY精神,利用ViewFlipper闭门土制了一个,下 ...