mysql进阶之MySQL查询

一、MySQL多表查询和子查询

别名:as

       表别名

       字段别名

       查询结果亦可命名别名

 

联结查询:事先将两张或多张表join,根据join的结果进行查询。

交叉联结(cross join):第一张表的每一行与第二张表的每一行做交叉相乘。

自然联结,也叫等值联结、内联结。两张表中的字段做等值关联。

自联结:

外联结:

       左外联结:只保留出现在左外联结运算之前左侧的关系中的元组。以左表为尊,右侧没有,右侧留空。

       右外联结:只保留出现在左外联结运算之前右侧的关系中的元组。以右表为尊,左侧没有,左侧留空。

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

mysql> select s.name,c.class from students as s RIGHT JOIN classes as c on s.classid=c.classid;
+---------------+----------------+
| name          | class          |
+---------------+----------------+
| Shi Potian| Shaolin Pai    |
| Wen Qingqing| Shaolin Pai    |
| Xu Zhu| Shaolin Pai    |
| Xiao Qiao| Shaolin Pai    |
| Shi Zhongyu| Emei Pai       |
| Xie Yanke| Emei Pai       |
| Tian Boguang| Emei Pai       |
| Yu Yutong| QingCheng Pai  |
| Xi Ren| QingCheng Pai  |
| Yue Lingshan| QingCheng Pai  |
| Lu Wushuang| QingCheng Pai  |
| Ding Dian| Wudang Pai     |
| Duan Yu| Wudang Pai     |
| Lin Chong| Wudang Pai     |
| Ma Chao| Wudang Pai     |
| Shi Qing| Riyue Shenjiao |
| Ren Yingying| Lianshan Pai   |
| Yuan Chengzhi | Lianshan Pai   |
| Xue Baochai| Lianshan Pai   |
| Huang Yueying | Lianshan Pai   |
| Lin Daiyu| Ming Jiao      |
| Hua Rong| Ming Jiao      |
| Diao Chan| Ming Jiao      |
| NULL| Xiaoyao Pai    |
+---------------+----------------+

24 rows in set (0.00 sec) 

子查询:在查询中嵌套查询,mysql子查询性能优化一般,应尽量避免使用,可使用联结查询替代。

       1、用于比较表达式(WHERE)中的子查询,其返回值只能唯一。

       2、用于EXISTS中的子查询,判断存在与否

       3、用于ININ list))中的子查询,判断存在与指定列表中。

       4、用于FROM中的子查询

              select
Alias_name.col1,Alias_name.col2… from (selectclause ) As Alias_name Where condition;
 

MySQL视图(view):存储下来的select语句,用于限定查询结果,隐藏表中的某些信息。将限定的查询结果从原表中获取,客户的查询 被限制在限定的结果中。

mysql> create VIEW testview as select
name,age from students;
mysql> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
|testview          |
| toc               |
+-------------------+
8 rows in set (0.01 sec)

查看视图中

mysql> select * from testview;
+---------------+-----+
| 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 |
+---------------+-----+

视图的状态与table的状态是有区别的,但其可以被当作表来使用,在插入数据或更新数据时,不应该基于view来实现。

mysql> show table status like
"testview"\G
*************************** 1. row
***************************
Name: testview
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
 Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
 Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)

删除视图,使用drop

mysql进阶之MySQL查询

select name,age from students where age > (select avg(age) from students);
select students.name,courses.course from students,courses,scores where students.StuID = scores.StuID 
and scores.CourseID = courses.CourseID and courses.courseid in (1,2,4,7);
select s.name,s.classid from (
select classid from students group by classid having count(ClassID)>=3) as
c,students as s where c.classid=s.classid and age>(select avg(age) from
students);
select s.name,s.classid,s.age
from students as s where classid is not null and s.age>(select avg(age) from
students) ;

来自为知笔记(Wiz)

原创文章,作者:M20-1钟明波,如若转载,请注明出处:http://www.178linux.com/59639

(0)
M20-1钟明波M20-1钟明波
上一篇 2016-11-18 19:01
下一篇 2016-11-18 19:22

相关推荐

  • 实时获取MySQL的TPS、QPS(输出到屏幕)

    这个脚本挺好用的,例如我们在主库执行了一个大事务,结果导致从库的show slave status\G 一直卡在 system lock状态,我们用下面这个脚本就能知道到底是hang住了,还是只是执行缓慢(反应在脚本执行结果里面就是qps\tps是不停变动的)。 当然,从库一直处于system lock 状态,一般是因为我们从库也设置双1导致,只要…

    Linux干货 2017-05-07
  • ansible基础应用

    Ansible基础 ansible是一款自动化运维工具,基于Python开发,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。 Ansible架构简介 ansible的核心就是ansible平台,ansible是高度模…

    Linux干货 2016-08-22
  • 第二周 博客作业

    1、linux上的文件管理类命令有哪些,其常用的使用方法及其相关示例。 文件管理命令:rm  mv cp       cp 复制命令       cp   源文件   目标文件           工作机制:如…

    Linux干货 2016-11-03
  • RAID 0 软件实现

    RAID 0 软件实现        RAID 0又称为Stripe或Striping,它代表了所有RAID级别中最高的存储性能。RAID 0提高存储性能的原理是把连续的数据分散到多个磁盘上存取,这样,系统有数据请求就可以被多个磁盘并行的执行,每个磁盘执行属于它自己的那部分数据请求。这种数据上的并行…

    Linux干货 2017-05-02
  • N21天天第十一周课程练习

    1、详细描述一次加密通讯的过程,结合图示最佳。 SSL协议基础: SSL协议位于TCP/IP协议与各种应用层协议之间,本身又分为两层: 1)SSL记录协议:建立在可靠传输层协议(TCP)之上,为上层协议提供数据封装、压缩、加密等基本功能。 2)SSL握手协议:在SSL记录协议之上,用于实际数据传输前,通讯双方进行身份认证、协商加密算法、交换加密密钥等。 SS…

    Linux干货 2016-10-09
  • 计算机简介

    计算机的简介 电子计算机(英语:computer),亦称电脑,是一种利用「电子学」原理,根据一系列指令对数据进行处理的工具计算机种类繁多,但实际来看,计算机总体上是处理信息的工具。计算机在组成上形式不一,早期计算机的体积足有一间房屋的大小,而今天某些嵌入式计算机可能比一副「扑克牌」还小。当然,即使在今天依然有大量体积庞大的巨型计算机为特别的[科学]计算或面向…

    Linux干货 2016-10-28