运维之数据库查询1(简单入门,从浅入深)通俗易懂
其实在青鸟学习了1年的专业技术,数据库都是一概而过的,基本没学到什么,所以到了工作岗位,而且我是做系统运维的,并且是游戏公司,所以数据库真的太重要,尤其数据库查询,根据数据库查询可以很快的查询到所使用的字段,最后介绍个 运维常用数据库工具。
select查询的基本结构
select 字段
from 表
where 过滤条件
groupby 分组条件
orderby 排序条件
having 过滤的第二条件
limit 限定结果条件
首先做例子 创建一个数据库和一个表
mysql> create database office 创建数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| accp |
| benet |
| cacti |
| mysql |
| office |
+--------------------+
6 rows in set (0.00 sec)
mysql> use office
Database changed
mysql> show tables;
+------------------+
| Tables_in_office |
+------------------+
| employ |
| offices |
+------------------+
2 rows in set (0.00 sec)
mysql> insert into fruits(f_id,s_id,f_name,f_price)VALUES ('a1',101,'apple',5.2), ('b2',101,'blackberry',10.2), ('c3',103,'erry',9.2), ('d4',101,'orange',11.2), ('e5',101,'melon',8.2), ('f6',102,'grape',8.5), ('g7',102,'berry',7.6), ('h8',101,'coconut',5.6), ('j9',103,'xxtt',11.6), ('y10',105,'xxxx',3.6);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0 在创建个表用来测试
1.单表查询
1,在select 语句中使用*号通配符查询所有字段
mysql> select * from fruits;
2,在select语句中制定所有字段比如查询f_id
mysql> select f_id from fruits;
3, 查询多个字段
mysql> select f_id,f_name from fruits;
4,查询指定记录
select 字段名1,字段名2,。。。。。 from 表名 where查询条件
操作 | = | <>,!= | < | <= | > | >== | BETWEEN | |
说明 | 相等 | 不相等 | 小于 | 小于或者等于 | 大于 | 大于或者等于 | 位于2值之间 |
比如查询价格为10.2元的名称?
mysql> SELECT f_name,f_price FROM fruits WHERE f_price=10.2;
比如查询名字为apple的价格?
mysql> SELECT f_name,f_price FROM fruits WHERE f_name='orange'; 名字要加‘’进去
比如查询价格小于10元?
mysql> SELECT f_name,f_price FROM fruits WHERE f_price<10;
+---------+---------+
| f_name | f_price |
+---------+---------+
| apple | 5.20 |
| erry | 9.20 |
| melon | 8.20 |
| grape | 8.50 |
| berry | 7.60 |
| coconut | 5.60 |
| xxxx | 3.60 |
+---------+---------+
7 rows in set (0.01 sec)
5,带IN关键字查询
s_id为101和102的记录
mysql> SELECT s_id,f_name,f_price FROM fruits WHERE s_id IN(101,102)
-> ORDER BY f_name; 排序出来
+------+------------+---------+
| s_id | f_name | f_price |
+------+------------+---------+
| 101 | apple | 5.20 |
| 102 | berry | 7.60 |
| 101 | blackberry | 10.20 |
| 101 | coconut | 5.60 |
| 102 | grape | 8.50 |
| 101 | melon | 8.20 |
| 101 | orange | 11.20 |
+------+------------+---------+
7 rows in set (0.00 sec)
6,相反的可以使用关键字NOT来检索不在条件范围内的记录。
查询所有s_id不等于101也不等于102的记录
mysql> SELECT s_id,f_name,f_price FROM fruits WHERE s_id NOT IN(101,102) ORDER BY f_name;
+------+--------+---------+
| s_id | f_name | f_price |
+------+--------+---------+
| 103 | erry | 9.20 |
| 103 | xxtt | 11.60 |
| 105 | xxxx | 3.60 |
+------+--------+---------+
3 rows in set (0.00 sec)
7,带BETWEEN AND的范围查询
查询价格在2元到10.2元之间的名称和价格?
mysql> SELECT f_name,f_price FROM fruits WHERE f_price BETWEEN 2.00 AND 10.20;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| blackberry | 10.20 |
| erry | 9.20 |
| melon | 8.20 |
| grape | 8.50 |
| berry | 7.60 |
| coconut | 5.60 |
| xxxx | 3.60 |
+------------+---------+
8 rows in set (0.00 sec)
查询这2个之外的价格 直接 加 NOT 在里面就可以了
mysql> SELECT f_name,f_price FROM fruits WHERE f_price NOT BETWEEN 2.00 AND 10.20;
+--------+---------+
| f_name | f_price |
+--------+---------+
| orange | 11.20 |
| xxtt | 11.60 |
+--------+---------+
2 rows in set (0.00 sec)
8,带LIKE的字符匹配查询
百分号通配符% 匹配任意长度的字符,甚至包括零字符
查找所有以b字母开头?
mysql> SELECT f_id,f_name FROM fruits WHERE f_name LIKE 'b%';
+------+------------+
| f_id | f_name |
+------+------------+
| b2 | blackberry |
| g7 | berry |
+------+------------+
2 rows in set (0.00 sec)
查询f_name 中 包含字母g的记录
mysql> SELECT f_id,f_name FROM fruits WHERE f_name LIKE '%g%';
+------+--------+
| f_id | f_name |
+------+--------+
| d4 | orange |
| f6 | grape |
+------+--------+
2 rows in set (0.00 sec)
查找以b开头 y结尾的记录
mysql> SELECT f_id,f_name FROM fruits WHERE f_name LIKE 'b%y';
+------+------------+
| f_id | f_name |
+------+------------+
| b2 | blackberry |
| g7 | berry |
+------+------------+
2 rows in set (0.00 sec)
下划线通配符 _ ,一次只能匹配任意一个字符。
查找以字母y结尾,且y前面只有4个字母的记录
mysql> SELECT f_id,f_name FROM fruits WHERE f_name LIKE '____y';
+------+--------+
| f_id | f_name |
+------+--------+
| g7 | berry |
+------+--------+
1 row in set (0.00 sec)
9,带AND的多条件查询 必须同时满足
查询s_id=101,f_price 大于等于5的名称
mysql> SELECT f_id,f_price,f_name,s_id FROM fruits WHERE s_id='101'AND f_price>=5;
+------+---------+------------+------+
| f_id | f_price | f_name | s_id |
+------+---------+------------+------+
| a1 | 5.20 | apple | 101 |
| b2 | 10.20 | blackberry | 101 |
| d4 | 11.20 | orange | 101 |
| e5 | 8.20 | melon | 101 |
| h8 | 5.60 | coconut | 101 |
+------+---------+------------+------+
5 rows in set (0.00 sec)
10,带OR的多条件查询
只需要满足一个即可和AND相反
查询s_id=101或者s_id=102的f_price,f_name
mysql> SELECT f_id,f_price,f_name,s_id FROM fruits WHERE s_id='101' OR s_id='102';
+------+---------+------------+------+
| f_id | f_price | f_name | s_id |
+------+---------+------------+------+
| a1 | 5.20 | apple | 101 |
| b2 | 10.20 | blackberry | 101 |
| d4 | 11.20 | orange | 101 |
| e5 | 8.20 | melon | 101 |
| f6 | 8.50 | grape | 102 |
| g7 | 7.60 | berry | 102 |
| h8 | 5.60 | coconut | 101 |
+------+---------+------------+------+
7 rows in set (0.00 sec)
OR可以和AND一起使用,但是在使用时应注意两者的优先级,由于AND的优先级高于OR,因此先对AND两边的操作数进行操作!
11,查询结果不重复,我们发现现在在查的话发现很多都是重复的
mysql> select s_id from fruits
-> ;
+------+
| s_id |
+------+
| 101 |
| 101 |
| 103 |
| 101 |
| 101 |
| 102 |
| 102 |
| 101 |
| 103 |
| 105 |
+------+
10 rows in set (0.00 sec)
所以避免重复 加上DISTINCT
mysql> select DISTINCT s_id from fruits;
+------+
| s_id |
+------+
| 101 |
| 103 |
| 102 |
| 105 |
+------+
4 rows in set (0.00 sec)
12,给查询结果排序
mysql> select f_name from fruits; 发现数据很乱
+------------+
| f_name |
+------------+
| apple |
| blackberry |
| erry |
| orange |
| melon |
| grape |
| berry |
| coconut |
| xxtt |
| xxxx |
+------------+
10 rows in set (0.00 sec)
这时我们使用 ORDER BY 进行排序
mysql> select f_name from fruits ORDER BY f_name;
+------------+
| f_name |
+------------+
| apple |
| berry |
| blackberry |
| coconut |
| erry |
| grape |
| melon |
| orange |
| xxtt |
| xxxx |
+------------+
10 rows in set (0.00 sec)
按字母顺序进行排序,abceg。。。
多列排序注意,首先第一列必须有相同值,这样才会排序第二列,否则将不再进行第二列排序。
如果通过降序可以 使用DESC进行降序方式排序
分组查询
1,GROUP BY 字段 HAVING 条件表达式
mysql> SELECT s_id,COUNT(*)AS Total FROM fruits GROUP BY s_id;
+------+-------+
| s_id | Total |
+------+-------+
| 101 | 5 |
| 102 | 2 |
| 103 | 2 |
| 105 | 1 |
+------+-------+
4 rows in set (0.00 sec) 查询出每个id 提供多少种水果。
根据s_id对表中的数据进行分组,显示结果。
mysql> SELECT s_id,GROUP_CONCAT(f_name)AS Names FROM fruits GROUP BY s_id;
+------+---------------------------------------+
| s_id | Names |
+------+---------------------------------------+
| 101 | apple,blackberry,coconut,orange,melon |
| 102 | berry,grape |
| 103 | erry,xxtt |
| 105 | xxxx |
+------+---------------------------------------+
4 rows in set (0.00 sec)
使用HAVING的过滤分组
根据s_id,对表中的数据进行分组,并显示种类大于1的分组信息
mysql> SELECT s_id,GROUP_CONCAT(f_name)AS Names FROM fruits GROUP BY s_id HAVING COUNT(f_name)>1;
+------+---------------------------------------+
| s_id | Names |
+------+---------------------------------------+
| 101 | apple,blackberry,coconut,orange,melon |
| 102 | berry,grape |
| 103 | erry,xxtt |
+------+---------------------------------------+
3 rows in set (0.00 sec)
在group by 子句中使用 with rollup
根据s_id对表中的数据进行分组,显示记录数量。
mysql> SELECT s_id,COUNT(*)AS Total FROM fruits GROUP BY s_id WITH ROLLUP;
+------+-------+
| s_id | Total |
+------+-------+
| 101 | 5 |
| 102 | 2 |
| 103 | 2 |
| 105 | 1 |
| NULL | 10 |
+------+-------+
5 rows in set (0.00 sec)
多字段分组:
根据s_id和f_name字段对表中的数据进行分组
mysql> select * from fruits group by s_id,f_name;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b2 | 101 | blackberry | 10.20 |
| h8 | 101 | coconut | 5.60 |
| e5 | 101 | melon | 8.20 |
| d4 | 101 | orange | 11.20 |
| g7 | 102 | berry | 7.60 |
| f6 | 102 | grape | 8.50 |
| c3 | 103 | erry | 9.20 |
| j9 | 103 | xxtt | 11.60 |
| y10 | 105 | xxxx | 3.60 |
+------+------+------------+---------+
10 rows in set (0.00 sec)
未完待续: 后面更新集合函数查找和子查询
常用工具:
PowerDesigner Navicat for MySQL
这样工具 百度上都能下到,也有手册!我也是刚学的!后续介绍方法!
数据库查询对运维工程很重要,而且要使用 一些常用的工具,PowerDesigner Navicat for MySQL,VMware vSphere Client,Remote Desktop Connection,WinSCP,SSH Secure Shell Client,Citrix XenCenter,Foxmail,亿图图示专家V7,目前我来公司所接触的基本工具,每一个都很重要!名字说出来之后,大家都知道怎么办了,找度娘!
来源:oschina
链接:https://my.oschina.net/u/4342750/blog/4322238