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.www58058.com/59639

(0)
M20-1鐘明波M20-1鐘明波
上一篇 2016-11-18
下一篇 2016-11-18

相關推薦

  • Linux-Basic—shell如何解釋命令的運行

    附件下載: Linux Basics-shell.pdf

    Linux干貨 2016-11-21
  • N25_第十三周作業

    1、建立samba共享,共享目錄為/data,要求:(描述完整的過程)  1)共享名為shared,工作組為magedu;  2)添加組develop,添加用戶gentoo,centos和ubuntu,其中gentoo和centos以develop為附加組,ubuntu不屬于develop組;密碼均為用戶名;  3)添加samb…

    Linux干貨 2017-03-06
  • linux做路由并實現路由轉發

    一、環境介紹 1.linux版本:CentOS6.8,CentOS7.2兩臺主機分別加載兩塊網卡,分別作為路由器的兩個端口 2.實驗在vmware虛擬機中完成 3.另有兩臺CentOS6.8和CentOS7.2作為兩個網段的主機 4.實驗圖示: 二、路由1(左)和路由2(右)的設置 1.路由1配置信息 eth0網卡:    &n…

    Linux干貨 2016-09-07
  • 踩踩Linux命令中的那些坑

    有時候面對命令的執行和條件的判斷,可能會遇到莫名奇妙的原因,可能是人為錯誤導致,也可能是命令本身的問題。

    Linux干貨 2017-11-23
  • 對inode的初步理解

    1.什么是inode?     inode中文譯作”索引節點“,是linux操作系統中的一種數據結構,用來存儲文件的元數據信息。在linux系統中每個文件都會分配一個inode,我們也可以把inode看作指針,它永遠指向文件的具體存儲位置。 2.inode中包含了什么信息? * inode 編號 * 用來識別文件類型,以及用于 …

    2017-07-18
  • lamp+nfs搭建wordpress

    一、前言 lamp是大多上公司常用的架構,本文將針對分離式的lamp+nfs來搭建一個簡單的wordpress網站。 二、架構圖 三、基本實現過程 3.1:dns搭建 由于這這是一個實驗,故使用yum搭建dns服務器 yum install -y bind 配置如下 dns主配置文件 dns輔助配置文件 zone文件 對所有服務…

    Linux干貨 2015-10-16
欧美性久久久久