mysql架构
查询用法
单表查询:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[SQL_CACHE | SQL_NO_CACHE]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE | LOCK IN SHARE MODE]
注意:如果单表的会就使用mysql的缓存,如果有很多mysql就可以使用memcache,运维mysql的缓存只有16兆。
DISTINCT: 数据去重;
MariaDB [hellodb]> select Gender from students;
+--------+
| Gender |
+--------+
| M |
| M |
| M |
| M |
| M |
| M |
| F |
| F |
| F |
| F |
| M |
| F |
| M |
| F |
| M |
| M |
| M |
| M |
| F |
| F |
| F |
| F |
| M |
| M |
| M |
| F |
+--------+
26 rows in set (0.00 sec)
MariaDB [hellodb]> select distinct Gender from students;
+--------+
| Gender |
+--------+
| M |
| F |
+--------+
SQL_CACHE: 显式指定存储查询结果于缓存之中;
SQL_NO_CACHE: 显式查询结果不予缓存;
query_cache_type的值为'ON'时,查询缓存功能打开;SELECT的结果符合缓存条件即会缓存,否则,不予缓存;
显式指定SQL_NO_CACHE,不予缓存;
query_cache_type的值为'DEMAND'时,查询缓存功能按需进行;显式指定SQL_CACHE的SELECT语句才会缓存;其它均不予缓存
MariaDB [hellodb]> show global variables like 'query%';
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| query_alloc_block_size | 8192 |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 16777216 |
| query_cache_strip_comments | OFF |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
| query_prealloc_size | 8192 |
+------------------------------+----------+
缓存命中率:缓存命中次数/命中次数+未命中次数
MariaDB [hellodb]> show global status like 'Qcache%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16751368 |
| Qcache_hits | 3 |
| Qcache_inserts | 11 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 557 |
| Qcache_queries_in_cache | 6 |
| Qcache_total_blocks | 16 |
+-------------------------+----------+
8 rows in set (0.00 sec)
MariaDB [hellodb]> show global status like 'com_se%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| Com_select | 819 |
| Com_set_option | 1995 |
+----------------+-------+
2 rows in set (0.00 sec)
字段显示可以使用别名:col1 AS alias1, col2 AS alias2, ...
WHERE子句:指明过滤条件以实现“选择”的功能:
过滤条件:布尔型表达式;
算术操作符:+, -, *, /, %
比较操作符:=, !=, <>, <=>, >, >=, <, <=
MariaDB [hellodb]> select Name,Age from students where age>20;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Yuan Chengzhi | 23 |
| Tian Boguang | 33 |
| Xu Zhu | 21 |
| Lin Chong | 25 |
| Hua Rong | 23 |
| Huang Yueying | 22 |
| Ma Chao | 23 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
+---------------+-----+
16 rows in set (0.03 sec)
MariaDB [hellodb]> select Name,Age from students where age+10>20;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Xi Ren | 19 |
| Lin Daiyu | 17 |
| Ren Yingying | 20 |
| Yue Lingshan | 19 |
| Yuan Chengzhi | 23 |
| Wen Qingqing | 19 |
| Tian Boguang | 33 |
| Lu Wushuang | 17 |
| Duan Yu | 19 |
| Xu Zhu | 21 |
| Lin Chong | 25 |
| Hua Rong | 23 |
| Xue Baochai | 18 |
| Diao Chan | 19 |
| Huang Yueying | 22 |
| Xiao Qiao | 20 |
| Ma Chao | 23 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
+---------------+-----+
26 rows in set (0.27 sec)
MariaDB [hellodb]> select Name,Age from students where age !=22;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Xi Ren | 19 |
| Lin Daiyu | 17 |
| Ren Yingying | 20 |
| Yue Lingshan | 19 |
| Yuan Chengzhi | 23 |
| Wen Qingqing | 19 |
| Tian Boguang | 33 |
| Lu Wushuang | 17 |
| Duan Yu | 19 |
| Xu Zhu | 21 |
| Lin Chong | 25 |
| Hua Rong | 23 |
| Xue Baochai | 18 |
| Diao Chan | 19 |
| Xiao Qiao | 20 |
| Ma Chao | 23 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
+---------------+-----+
23 rows in set (0.00 sec)
BETWEEN min_num AND max_num
IN (element1, element2, ...)
IS NULL
IS NOT NULL
LIKE:%: 任意长度的任意字符;
_:任意单个字符;
RLIKE,REGEXP:匹配字符串可用正则表达式书写模式;
MariaDB [hellodb]> select Name,Age from students where age in (22,100);
+---------------+-----+
| Name | Age |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Huang Yueying | 22 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
+---------------+-----+
5 rows in set (0.01 sec)
MariaDB [hellodb]> select Name,ClassID from students where ClassID is null;
+---------------+---------+
| Name | ClassID |
+---------------+---------+
| Xu Xian | NULL |
| Sun Dasheng | NULL |
| jingjiao king | NULL |
+---------------+---------+
3 rows in set (0.00 sec)
逻辑操作符:NOT;AND;OR;XOR--两者相同为假
GROUP:根据指定的条件把查询结果进行“分组”以用于做“聚合”运算:
avg(), max(), min(), count(), sum()
MariaDB [hellodb]> select * FROM students group by Gender;
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
+-------+-------------+-----+--------+---------+-----------+
2 rows in set (0.00 sec)
MariaDB [hellodb]> select avg(Age),Gender from students group by Gender;
+----------+--------+
| avg(Age) | Gender |
+----------+--------+
| 26.3636 | F |
| 33.0000 | M |
+----------+--------+
2 rows in set (0.00 sec)
HAVING: 对分组聚合运算后的结果指定过滤条件;是针对group by做过滤
MariaDB [hellodb]> select avg(Age) as aage,Gender from students group by Gender having aage>30;
+---------+--------+
| aage | Gender |
+---------+--------+
| 33.0000 | M |
+---------+--------+
1 row in set (0.00 sec)
MariaDB [hellodb]> select count(StuID) as nos,ClassID from students group by ClassID;
+-----+---------+
| nos | ClassID |
+-----+---------+
| 3 | NULL |
| 4 | 1 |
| 3 | 2 |
| 4 | 3 |
| 4 | 4 |
| 1 | 5 |
| 4 | 6 |
| 3 | 7 |
+-----+---------+
8 rows in set (0.00 sec)
MariaDB [hellodb]> select count(StuID) as nos,ClassID from students group by ClassID having nos>3;
+-----+---------+
| nos | ClassID |
+-----+---------+
| 4 | 1 |
| 4 | 3 |
| 4 | 4 |
| 4 | 6 |
+-----+---------+
4 rows in set (0.00 sec)
ORDER BY: 根据指定的字段对查询结果进行排序;
升序:ASC
降序:DESC
MariaDB [hellodb]> select Name,Age from students order by Age;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Lu Wushuang | 17 |
| Lin Daiyu | 17 |
| Xue Baochai | 18 |
| Wen Qingqing | 19 |
| Duan Yu | 19 |
| Yue Lingshan | 19 |
| Xi Ren | 19 |
| Diao Chan | 19 |
| Ren Yingying | 20 |
| Xiao Qiao | 20 |
| Xu Zhu | 21 |
| Huang Yueying | 22 |
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Yuan Chengzhi | 23 |
| Hua Rong | 23 |
| Ma Chao | 23 |
| Lin Chong | 25 |
| Yu Yutong | 26 |
| Xu Xian | 27 |
| Ding Dian | 32 |
| Tian Boguang | 33 |
| Shi Qing | 46 |
| Xie Yanke | 53 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
+---------------+-----+
26 rows in set (0.00 sec)
MariaDB [hellodb]> select Name,Age from students order by Age desc;
+---------------+-----+
| Name | Age |
+---------------+-----+
| jingjiao king | 100 |
| Sun Dasheng | 100 |
| Xie Yanke | 53 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Ding Dian | 32 |
| Xu Xian | 27 |
| Yu Yutong | 26 |
| Lin Chong | 25 |
| Ma Chao | 23 |
| Hua Rong | 23 |
| Yuan Chengzhi | 23 |
| Huang Yueying | 22 |
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xu Zhu | 21 |
| Ren Yingying | 20 |
| Xiao Qiao | 20 |
| Xi Ren | 19 |
| Duan Yu | 19 |
| Diao Chan | 19 |
| Yue Lingshan | 19 |
| Wen Qingqing | 19 |
| Xue Baochai | 18 |
| Lin Daiyu | 17 |
| Lu Wushuang | 17 |
+---------------+-----+
26 rows in set (0.00 sec)
MariaDB [hellodb]> select Name,Age from students order by Age desc limit 10;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Sun Dasheng | 100 |
| jingjiao king | 100 |
| Xie Yanke | 53 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Ding Dian | 32 |
| Xu Xian | 27 |
| Yu Yutong | 26 |
| Lin Chong | 25 |
| Ma Chao | 23 |
+---------------+-----+
10 rows in set (0.00 sec)
MariaDB [hellodb]> select Name,Age from students order by Age desc limit 10,10;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Hua Rong | 23 |
| Yuan Chengzhi | 23 |
| Huang Yueying | 22 |
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xu Zhu | 21 |
| Ren Yingying | 20 |
| Xiao Qiao | 20 |
| Xi Ren | 19 |
| Duan Yu | 19 |
+---------------+-----+
LIMIT [[offset,]row_count]:对查询的结果进行输出行数数量限制;
[FOR UPDATE | LOCK IN SHARE MODE]对查询结果中的数据请求施加“锁”:
FOR UPDATE: 写锁,排他锁;其他人不能读和写
LOCK IN SHARE MODE: 读锁,共享锁
--------------------------------------------------------------------------
多表查询
交叉连接:笛卡尔乘积;
内连接:
等值连接:表之间的字段“等值”连接;
MariaDB [hellodb]> select * from students,teachers where students.TeacherID=teachers.TID;
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name | Age | Gender | ClassID | TeacherID | TID | Name | Age | Gender |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| 5 | Yu Yutong | 26 | M | 3 | 1 | 1 | Song Jiang | 45 | M |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 3 | Miejue Shitai | 77 | F |
| 4 | Ding Dian | 32 | M | 4 | 4 | 4 | Lin Chaoying | 93 | F |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
3 rows in set (0.00 sec)
MariaDB [hellodb]> select * from students,teachers where students.TeacherID=teachers.TID;
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name | Age | Gender | ClassID | TeacherID | TID | Name | Age | Gender |
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| 5 | Yu Yutong | 26 | M | 3 | 1 | 1 | Song Jiang | 45 | M |
| 26 | jingjiao king | 100 | F | NULL | 1 | 1 | Song Jiang | 45 | M |
| 27 | yingjiao King | 98 | M | NULL | 2 | 2 | Zhang Sanfeng | 94 | M |
| 1 | Shi Zhongyu | 22 | M | 2 | 3 | 3 | Miejue Shitai | 77 | F |
| 4 | Ding Dian | 32 | M | 4 | 4 | 4 | Lin Chaoying | 93 | F |
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
5 rows in set (0.00 sec)
MariaDB [hellodb]> select s.Name as StuName,t.Name as TeaName from students as s,teachers as t where s.Stuid=t.TID;
+-------------+---------------+
| StuName | TeaName |
+-------------+---------------+
| Shi Zhongyu | Song Jiang |
| Shi Potian | Zhang Sanfeng |
| Xie Yanke | Miejue Shitai |
| Ding Dian | Lin Chaoying |
+-------------+---------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> select s.Name as StuName,t.Name as TeaName from students as s,teachers as t where s.Stuid=t.TID;
+-------------+---------------+
| StuName | TeaName |
+-------------+---------------+
| Shi Zhongyu | Song Jiang |
| Shi Potian | Zhang Sanfeng |
| Xie Yanke | Miejue Shitai |
| Ding Dian | Lin Chaoying |
+-------------+---------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]> explain select s.Name as StuName,t.Name as TeaName from students as s,teachers as t where s.Stuid=t.TID;
+------+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------------+
| 1 | SIMPLE | t | ALL | PRIMARY | NULL | NULL | NULL | 4 | |
| 1 | SIMPLE | s | eq_ref | PRIMARY | PRIMARY | 4 | hellodb.t.TID | 1 | Using index condition |
+------+-------------+-------+--------+---------------+---------+---------+---------------+------+-----------------------+
2 rows in set (0.00 sec)
MariaDB [hellodb]> explain select s.Name as StuName,t.Name as TeaName from students as s,teachers as t where s.Stuid=t.TID\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 4
Extra:
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: s
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: hellodb.t.TID
rows: 1
Extra: Using index condition
2 rows in set (0.00 sec)
外连接:
左外连接: 已左表的数据为基础向右表等值连接
FROM tb1 LEFT JOIN tb2 ON tb1.col=tb2.col
MariaDB [hellodb]> select s.Name,c.Class from students as s left join classes as c on s.ClassID=c.ClassID;
+---------------+----------------+
| Name | Class |
+---------------+----------------+
| Shi Zhongyu | Emei Pai |
| Shi Potian | Shaolin Pai |
| Xie Yanke | Emei Pai |
| Ding Dian | Wudang Pai |
| Yu Yutong | QingCheng Pai |
| Shi Qing | Riyue Shenjiao |
| Xi Ren | QingCheng Pai |
| Lin Daiyu | Ming Jiao |
| Ren Yingying | Lianshan Pai |
| Yue Lingshan | QingCheng Pai |
| Yuan Chengzhi | Lianshan Pai |
| Wen Qingqing | Shaolin Pai |
| Tian Boguang | Emei Pai |
| Lu Wushuang | QingCheng Pai |
| Duan Yu | Wudang Pai |
| Xu Zhu | Shaolin Pai |
| Lin Chong | Wudang Pai |
| Hua Rong | Ming Jiao |
| Xue Baochai | Lianshan Pai |
| Diao Chan | Ming Jiao |
| Huang Yueying | Lianshan Pai |
| Xiao Qiao | Shaolin Pai |
| Ma Chao | Wudang Pai |
| Xu Xian | NULL |
| Sun Dasheng | NULL |
| jingjiao king | NULL |
| yingjiao King | NULL |
+---------------+----------
右外连接
FROM tb1 RIGHT JOIN tb2 ON tb1.col=tb2.col
MariaDB [hellodb]> select s.Name,c.Class from students as s right join classes as c on s.ClassID=c.ClassID;
+---------------+----------------+
| Name | Class |
+---------------+----------------+
| Shi Zhongyu | Emei Pai |
| Shi Potian | Shaolin Pai |
| Xie Yanke | Emei Pai |
| Ding Dian | Wudang Pai |
| Yu Yutong | QingCheng Pai |
| Shi Qing | Riyue Shenjiao |
| Xi Ren | QingCheng Pai |
| Lin Daiyu | Ming Jiao |
| Ren Yingying | Lianshan Pai |
| Yue Lingshan | QingCheng Pai |
| Yuan Chengzhi | Lianshan Pai |
| Wen Qingqing | Shaolin Pai |
| Tian Boguang | Emei Pai |
| Lu Wushuang | QingCheng Pai |
| Duan Yu | Wudang Pai |
| Xu Zhu | Shaolin Pai |
| Lin Chong | Wudang Pai |
| Hua Rong | Ming Jiao |
| Xue Baochai | Lianshan Pai |
| Diao Chan | Ming Jiao |
| Huang Yueying | Lianshan Pai |
| Xiao Qiao | Shaolin Pai |
| Ma Chao | Wudang Pai |
| NULL | Xiaoyao Pai |
+---------------+----------------+
24 rows in set (0.00 sec)
-------------------------------------------------------------------------
子查询:在查询语句嵌套着查询语句
注意:不建议使用
基于某语句的查询结果再次进行的查询
用在WHERE子句中的子查询:
(1) 用于比较表达式中的子查询;子查询仅能返回单个值;
SELECT Name,Age FROM students WHERE Age>(SELECT avg(Age) FROM students);
2) 用于IN中的子查询:子查询应该单键查询并返回一个或多个值从构成列表;
SELECT Name,Age FROM students WHERE Age IN (SELECT Age FROM teachers);
(3) 用于EXISTS;
用于FROM子句中的子查询;
使用格式:SELECT tb_alias.col1,... FROM (SELECT clause) AS tb_alias WHERE Clause;
MariaDB [hellodb]> SELECT s.aage,s.ClassID FROM (SELECT avg(Age) AS aage,ClassID FROM students WHERE ClassID IS NOT NULL GROUP BY ClassID) AS s WHERE s.aage>30;
+---------+---------+
| aage | ClassID |
+---------+---------+
| 36.0000 | 2 |
| 46.0000 | 5 |
+---------+---------+
2 rows in set (0.00 sec)
-----------------------------------------------------------------------------
联合查询:UNION
注意:字段类型要一致
MariaDB [hellodb]> SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers;
+---------------+-----+
| Name | Age |
+---------------+-----+
| Shi Zhongyu | 22 |
| Shi Potian | 22 |
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Xi Ren | 19 |
| Lin Daiyu | 17 |
| Ren Yingying | 20 |
| Yue Lingshan | 19 |
| Yuan Chengzhi | 23 |
| Wen Qingqing | 19 |
| Tian Boguang | 33 |
| Lu Wushuang | 17 |
| Duan Yu | 19 |
| Xu Zhu | 21 |
| Lin Chong | 25 |
| Hua Rong | 23 |
| Xue Baochai | 18 |
| Diao Chan | 19 |
| Huang Yueying | 22 |
| Xiao Qiao | 20 |
| Ma Chao | 23 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
| jingjiao king | 100 |
| yingjiao King | 98 |
| Song Jiang | 45 |
| Zhang Sanfeng | 94 |
| Miejue Shitai | 77 |
| Lin Chaoying | 93 |
+---------------+-----+
31 rows in set (0.00 sec)