數據庫鏈接關係圖
mysql
學生表與班級表取左外鏈接sql
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';