MySQL之单表查询

创建表

# 创建表
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
); # 插入数据
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','','instructor','teach',5000,501,100),
('tom','male','','instructor','teach',5500,501,100),
('robin','male','','instructor','teach',8000,501,100),
('alice','female','','instructor','teach',7200,501,100),
('tianyun','male','','hr','hrcc',600,502,101),
('harry','male','','hr',NULL,6000,502,101),
('emma','female','','sale','salecc',20000,503,102),
('christine','female','','sale','salecc',2200,503,102),
('zhuzhu','male','','sale',NULL,2200,503,102),
('gougou','male','','sale','',2200,503,102); # 查看表结构
mysql> desc employee5;
+-----------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | NO | | NULL | |
| job_description | varchar(100) | YES | | NULL | |
| salary | double(15,2) | NO | | NULL | |
| office | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+-----------------+-----------------------+------+-----+---------+----------------+

查询语法

SELECT 字段1,字段2... FROM 表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数;

查看表中所有数据

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 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)

简单查询

简单查询

mysql> SELECT * FROM employee5;

mysql> SELECT name, salary, dep_id FROM employee5;

去重DISTINCT

mysql> SELECT post FROM employee5;

mysql> SELECT DISTINCT post  FROM employee5;

注:不能部分使用DISTINCT,通常仅用于某一字段。

通过四则运算查询

mysql> SELECT name, salary, salary*14 FROM employee5;

mysql> SELECT name, salary, salary*14 AS Annual_salary FROM employee5;

mysql> SELECT name, salary, salary*14 Annual_salary FROM employee5;

定义显示格式

CONCAT() 函数用于连接字符串

mysql> SELECT CONCAT(name, ' annual salary: ', salary*14)  AS Annual_salary FROM employee5;

条件查询

a、语法
select * from 表名 where 条件
b、比较运算符
大于 小于 大于等于 小于等于 不等于
> < >= <= !=或<> c、逻辑运算符
并且 或者 非
and or not d、模糊查询
like
% 表示任意多个任意字符
_ 表示一个任意字符 e、范围查询
in 表示在一个非连续的范围内
between...and... 表示在一个连续的范围内 f、空判断
判断空:is null
判断非空:is not null g、优先级
小括号,not 比较运算符, 逻辑运算符
and比or优先级高,如果同时出现并希望先选or,需要结合()来使用

单条件查询

mysql> SELECT name,post FROM employee5 WHERE post='hr';

多条件查询

mysql> SELECT name,salary FROM employee5 WHERE post='hr' AND salary>5000;

关键字 BETWEEN AND查询

mysql> SELECT name,salary FROM employee5 WHERE salary BETWEEN 5000 AND 15000;

mysql> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000;

关键字 IS NULL 查询

mysql> SELECT name,job_description FROM employee5 WHERE job_description IS NULL;

mysql> SELECT name,job_description FROM employee5 WHERE job_description IS NOT NULL;

mysql> SELECT name,job_description FROM employee5 WHERE job_description='';

关键字IN集合查询

mysql> SELECT name, salary FROM employee5 WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ;

mysql> SELECT name, salary FROM employee5 WHERE salary IN (4000,5000,6000,9000) ;

mysql> SELECT name, salary FROM employee5 WHERE salary NOT IN (4000,5000,6000,9000) ;

关键字LIKE模糊查询

通配符’%’
mysql> SELECT * FROM employee5 WHERE name LIKE 'al%'; 通配符’_’
mysql> SELECT * FROM employee5 WHERE name LIKE 'al___';

查询排序

按单列排序

mysql> SELECT * FROM employee5 ORDER BY salary;

mysql> SELECT name, salary FROM employee5 ORDER BY salary ASC;

mysql> SELECT name, salary FROM employee5 ORDER BY salary DESC;

按多列排序

mysql> SELECT * FROM employee5 ORDER BY hire_date DESC,salary ASC;

# 先按入职时间,再按薪水排序
mysql> SELECT * FROM employee5 ORDER BY hire_date DESC, salary DESC; # 先按职位,再按薪水排序
mysql> SELECT * FROM employee5 ORDER BY post, salary DESC;

分页查询 limit

mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 5;      //默认初始位置为0

mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 0,5;

mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 3,5;      //从第4条开始,共显示5条

聚合函数查询

    a、count(*)       表示计算总行数,括号中可以写*和列名
b、max(列) 表示求此列的最大值
c、min(列) 表示求此列的最小值
d、sun(列) 表示求此列的和
e、avg(列) 表示求此列的平均值 mysql> SELECT COUNT(*) FROM employee5;
mysql> SELECT COUNT(*) FROM employee5 WHERE dep_id=101;
mysql> SELECT MAX(salary) FROM employee5;
mysql> SELECT MIN(salary) FROM employee5;
mysql> SELECT AVG(salary) FROM employee5;
mysql> SELECT SUM(salary) FROM employee5;
mysql> SELECT SUM(salary) FROM employee5 WHERE dep_id=101;

分组查询

单独使用GROUP BY关键字分组
mysql> SELECT post FROM employee5 GROUP BY post;
注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数

GROUP BY关键字和group_concat()函数一起使用

# 按照id分组,并查看组内成员
mysql> SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id; mysql> SELECT dep_id,GROUP_CONCAT(name) as emp_members FROM employee5 GROUP BY dep_id;

GROUP BY和集合函数一起使用

# 按照dep_id 分组, 并计算组内成员工资总和
mysql> SELECT dep_id,SUM(salary) FROM employee5 GROUP BY dep_id; # 按照dep_id分组,并计算组内成员工资平均值
mysql> SELECT dep_id,AVG(salary) FROM employee5 GROUP BY dep_id;

正则表达式查询

mysql> SELECT * FROM employee5 WHERE name REGEXP '^j';

mysql> SELECT * FROM employee5 WHERE salary REGEXP '[5]+.*';

mysql> SELECT * FROM employee5 WHERE salary REGEXP '[5]{2}.*';

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

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  6. mysql 单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数   二 ...

  7. SQL学习笔记四(补充-1)之MySQL单表查询

    阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录 ...

  8. python开发mysql:单表查询&amp;多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  9. 0x06 MySQL 单表查询

    一 单表查询语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键字 ...

随机推荐

  1. DBCP数据源连接池实现原理分析

    前些天在调试公司系统的时候发现这样的一个问题:mysql数据库服务停止一段时间后再次重启后吗,tomcat服务无法请求数据库服务,调试了半天对这个问题进行定位解决,期间也搞了很多有关mysql数据库的 ...

  2. 【MySQL】MySQL锁和隔离级别浅析二 之 INSERT

    最近在整理线上性能时,发现一台线上DB出现两个insert产生的死锁问题 ------------------------ LATEST DETECTED DEADLOCK ------------- ...

  3. C# Ajax 手机发送短信验证码 校验验证码 菜鸟级别实现方法

    1.Ajax请求处理页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  4. MySql启动提示:The server quit without updating PID file(…)失败

    在网上找了很多 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data”  ...

  5. PHP简单MVC架构

    http://blog.csdn.net/haiqiao_2010/article/details/12166283 由于需要搭建一个简单的框架来进行API接口开发,所以简单的mvc框架当然是首选.最 ...

  6. curl 基本使用简介

    curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...

  7. C# 合并DLL, 合并DLL进入EXE

    原文:C# 合并DLL, 合并DLL进入EXE 使用方法非常简单 在项目属性窗口中,选择"生成事件",在"生成后事件命令行"下的文本框中输入 ilmerge / ...

  8. Document Classification

    Natural Language Processing with Python Chapter 6.1 由于nltk.FreqDist的排序问题,获取电影文本特征词的代码有些微改动. import n ...

  9. spring中自定义Event事件的使用和浅析

    在我目前接触的项目中,用到了许多spring相关的技术,框架层面的spring.spring mvc就不说了,细节上的功能也用了不少,如schedule定时任务.Filter过滤器. intercep ...

  10. 3-2 模板语法(vue中的内容写法)

    插值表达式.v-text.v-html的用法