码迷,mamicode.com
首页 > 数据库 > 详细

mysql基本操作

时间:2018-02-06 23:04:38      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:dai   左外连接   分享图片   dia   oss   基本   .com   uid   sel   

数据库连接关系图
技术分享图片

学生表与班级表取左外连接

select s.stuid,s.name,c.class from students as s left join classes as c on s.classid=c.classid;

+-------+---------------+----------------+
| stuid | name          | class          |
+-------+---------------+----------------+
|     1 | Shi Zhongyu   | Emei Pai       |
|     2 | Shi Potian    | Shaolin Pai    |
|     3 | Xie Yanke     | Emei Pai       |
|     4 | Ding Dian     | Wudang Pai     |
|     5 | Yu Yutong     | QingCheng Pai  |
|     6 | Shi Qing      | Riyue Shenjiao |
|     7 | Xi Ren        | QingCheng Pai  |
|     8 | Lin Daiyu     | Ming Jiao      |
|     9 | Ren Yingying  | Lianshan Pai   |
|    10 | Yue Lingshan  | QingCheng Pai  |
|    11 | Yuan Chengzhi | Lianshan Pai   |
|    12 | Wen Qingqing  | Shaolin Pai    |
|    13 | Tian Boguang  | Emei Pai       |
|    14 | Lu Wushuang   | QingCheng Pai  |
|    15 | Duan Yu       | Wudang Pai     |
|    16 | Xu Zhu        | Shaolin Pai    |
|    17 | Lin Chong     | Wudang Pai     |
|    18 | Hua Rong      | Ming Jiao      |
|    19 | Xue Baochai   | Lianshan Pai   |
|    20 | Diao Chan     | Ming Jiao      |
|    21 | Huang Yueying | Lianshan Pai   |
|    22 | Xiao Qiao     | Shaolin Pai    |
|    23 | Ma Chao       | Wudang Pai     |
# |    24 | Xu Xian       | NULL           |
# |    25 | Sun Dasheng   | NULL           |
+-------+---------------+----------------+

学生表与班级表取左外连接,并且减去学生表与课程表交集部分
select s.stuid,s.name,c.class from students as s left join classes as c on s.classid=c.classid where c.classid is NULL;

+-------+-------------+-------+
| stuid | name        | class |
+-------+-------------+-------+
|    24 | Xu Xian     | NULL  |
|    25 | Sun Dasheng | NULL  |
+-------+-------------+-------+

计算女生成绩的平均值
select avg(age) from students group by gender having gender=‘F‘;

mysql基本操作

标签:dai   左外连接   分享图片   dia   oss   基本   .com   uid   sel   

原文地址:http://blog.51cto.com/11034229/2069616

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!