日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql带条件查询,联表查询

發布時間:2023/12/13 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql带条件查询,联表查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

---恢復內容開始---

1,用于設定所select出來的數據是否允許出現重復行(完全相同的數據行)

all:允許出現——默認不寫就是All(允許的)。

distinct:不允許出現——就是所謂的“消除重復行”

2,where:條件

3,group by:分組依據 后面加表的字段名,通常只進行一個字段的分組

mysql表查詢語法形式:select [all | distinct] 字段名或表達式 from 表名 [where] [group by] [having] [order by] [limit];

練習題:共有下面四張表??? 學生表:student? 教師表:teacher? 課程表:course 成績表:score

????? ????

1,查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數

--操作表score,以cno分組并且cno是以3開頭,取出各個cno的總數,及與cno對應的degree的平均值

select count(cno),avg(degree) from score where cno like '3%' group by cno;

?

--從上面的虛擬表中找到cno總數大于5的

select * from

(select count(cno) a,avg(degree) b from score where cno like '3%' group by cno) c

where a>5;

2,查詢所有學生的Sno、Cname和Degree列

--找到student的sno
?select sno from student;


?--找到score 的sno,degree,cno
?select sno,degree,cno from score;


?--找到course的cno
?select cno,cname from course;


?--組成新表cno sno degree
?select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a? join (select sno,sname from student) b on a.sno=b.sno;


?--組成有cname cno sn degree sname的表
?select d.cname,e.cno,e.sno,e.degree,e.sname
?from
?? (select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a? join (select sno,sname from student) b on a.sno=b.sno) as e
? join
? (select cname,cno from course) as d
? on d.cno=e.cno;

3,查詢“95033”班學生的平均分

--從student取sno class,條件是class是95033的

select sno,class from student where class='95033';

--取出score中sno degree

select sno,degree from score;

--將上面兩張表組成一張,取degree的平均值

select avg(degree) from

(select sno,class from student where class='95033') a

join

(select sno,degree from score) b

on a.sno=b.sno;

4,查詢選修“3-105”課程的成績高于“109”號同學成績的所有同學的記錄

--將109號的成績取出

select degree from score where sno='109' and cno='3-105';

-- 得出最終表

select * from score

where

degree>(select degree from score where sno='109' and cno='3-105')

and cno='3-105';

5,查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列

--找到student中sno為108 的Sbirthday
select year(sbirthday) from student where sno='108';


-- 得出最終表
select sno,sname,sbirthday from student

where

year(sbirthday)=(select year(sbirthday) from student where sno='108');

?6.查詢“張旭“教師任課的學生成績(姓名)

--找到teacher中tname為張旭的tno

select tno from teacher where tname='張旭';

--找到course中tno和teacher中tname為張旭的tno相同的cno

select cno from course where tno=(select tno from teacher where tname='張旭');

--找到score中cno為6-166的sno cno degree

select sno,cno,degree from score

where

cno=(select cno from course where tno=(select tno from teacher where tname='張旭'));

--找到student表中與上表sno相同的sname與上表組成新表

select a.sname,b.sno,b.cno,b.degree from

(select sno,sname from student) a

join

(select sno sno,cno,degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='張旭'))) b

on a.sno=b.sno;

7,查詢考計算機導論的學生成績
--找到course中cname為計算機導論的cno
select cno from course where cname='計算機導論';


--找到score中cno與上表中相同時的sno degree
select sno,degree from score where cno=(select cno from course where cname='計算機導論');


-- 得出最終表
select b.sname,a.sno,a.degree from

(select sno,degree from score where

cno=(select cno from course where cname='計算機導論')) a join (select sname,sno from student) b

on a.sno=b.sno;


8,查詢李誠老師教的課程名稱
select tno from teacher where tname='李誠';
select cname,cno from course where tno=(select tno from teacher? where tname='李誠');


9,查詢選修某課程的同學人數多于5人的教師姓名
--score以cno分組,統計cno的數量
select count(cno) a,cno from score group by cno;


--找到上表中cno數量大于5的的cno
select cno from (select count(cno) a,cno from score group by cno) b where a>5;


--找到與上表cno一致的表course表中的tno
select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5);


--與上表tno一致的表teacher中的tname
select tname from teacher where tno=(select tno from course where cno=(select cno from (select count(cno) a,cno from score group by cno) b where a>5));


10,查詢95033班和95031班全體學生的記錄
--找到表score中的sno和cno 和degree
select sno,cno,degree from score;
--將上表和student組合
select a.sno,a.sname,a.ssex,a.sbirthday,a.class,b.cno,b.degree from

(select * from student) a join (select cno,degree,sno from score) b

on a.sno=b.sno;


11,查詢存在有85分以上成績的課程Cno
select cno,degree from score where degree>85;


12,查詢出“計算機系“教師所教課程的成績表
--找到teacher中的tno tname
select tno,tname from teacher where dapart='計算機系';
--找到course中tno和上表相同的cno
select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='計算機系') b on a.tno=b.tno;
--找到score中cno與上表相同的degree
select c.sno,c.cno,c.degree,d.tname from (select sno,cno,degree from score) c join (select a.cno,b.tno,b.tname from (select tno,cno from course) a join (select tno,tname from teacher where dapart='計算機系') b on a.tno=b.tno) d on c.cno=d.cno;


13,查詢選修編號為“3-105“課程且成績至少高于選修編號為“3-245”的同學?? ?的Cno、Sno和Degree,并按Degree從高到低次序排序
--score中找到cno=3-245的degree,取出最小值
select min(degree) from score where cno='3-245';
--score中找到cno=3-105的cno sno degree,并且degree大于上表的degree
select cno,sno,degree from

score where cno='3-105'

and degree>(select min(degree) from score where cno='3-245')

order by degree desc;


14,查詢選修編號為“3-105”且成績高于選修編號為“3-245”課程的同學的?? ?Cno、Sno和Degree.
--找到score中cno=3-245中的degree的最大值
select max(degree) from score where cno='3-245';
--找到score中cno=3-105的cno sno degree
select cno,sno,degree from

score where

degree>(select max(degree) from score where cno='3-245') order by degree asc;


15,查詢所有教師和同學的name、sex和birthday
select tname,tsex,tbirthday from teacher
union
select sname,ssex,sbirthday from student;
16,查詢所有“女”教師和“女”同學的name、sex和birthday
select tname,tsex,tbirthday from teacher where tsex='女'
union
select sname,ssex,sbirthday from student where ssex='女';


17,查詢成績比該課程平均成績低的同學的成績表
select avg(degree) from score where cno='3-105';
select avg(degree) from score where cno='3-245';
select avg(degree) from score where cno='6-166';
select * from score where cno='3-105' and degree<(select avg(degree) from score where cno='3-105')
union
select * from score where cno='3-245' and degree<(select avg(degree) from score where cno='3-245')
union
select * from score where cno='6-166' and degree<(select avg(degree) from score where cno='6-166');


18,查詢所有任課教師的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


19,查詢所有未講課的教師的Tname和Depart
select a.tname,a.dapart from
(select tno,tname,dapart from teacher) a join
(select tno from course) b
on a.tno=b.tno;


20,查詢至少有2名男生的班號
select count(ssex),class from student where ssex='男' group by class;


select b.class from (select count(ssex) a,class from student where ssex='男' group by class) b where a>=2;


21,查詢Student表中不姓“王”的同學記錄
select * from student where sname not like '王%';


22,查詢Student表中每個學生的姓名和年齡
select? sname,year(now())-year(sbirthday) from student;


23,查詢Student表中最大和最小的Sbirthday日期值
select min(day(sbirthday)),max(day(sbirthday)) from student;


24,以班號和年齡從大到小的順序查詢Student表中的全部記錄
select * from student order by class desc,date(sbirthday) asc;


25,查詢“男”教師及其所上的課程
select tno from teacher where tsex='男';
select a.tno,b.cname,a.tname from (select tno,tname from teacher where tsex='男') a join (select * from course) b on a.tno=b.tno;

?

轉載于:https://www.cnblogs.com/wfc139/p/8939605.html

總結

以上是生活随笔為你收集整理的mysql带条件查询,联表查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青青草原在线免费 | 高跟鞋调教—视频|vk | 婷婷综合激情网 | 97蜜桃网| 精品视频一区二区在线观看 | 免费人成年激情视频在线观看 | 亚洲欧美综合一区二区 | 琪琪电影午夜理论片八戒八戒 | 日皮视频免费看 | 懂色tv| 国产视频一区二区三区在线观看 | 人妻天天爽夜夜爽一区二区三区 | 国产一区二区三区电影在线观看 | 日韩欧美精品免费 | 日本黄色小说 | 成人黄色免费视频 | av第一区| 奇米影视第四色777 波多野结衣一区二区三区免费视频 | 日本电影大尺度免费观看 | 亚洲色图影院 | 日韩专区av | 一级片视频网站 | 成人黄页 | 高h视频在线播放 | 一级特黄妇女高潮2 | 国产盗摄av | 久久影院午夜 | 亚洲av成人无码久久精品老人 | 金鱼妻日剧免费观看完整版全集 | 真人抽搐一进一出视频 | 两个女人互添下身爱爱 | 无码人妻丰满熟妇区bbbbxxxx | 黄色不卡| 男人av影院 | 亚洲成人伊人 | 国产视频中文字幕 | 嫩模一区二区三区 | 欧美日韩国产麻豆 | 中文字幕人妻精品一区 | 中文字幕+乱码+中文乱码91 | 日韩欧美高清dvd碟片 | 亚洲一区成人在线 | 色久综合网 | 2024av视频 | 亚洲av无码国产精品色午夜 | 乱日视频| 嫩草av在线| 亚洲影院中文字幕 | 久久久久久久久久久久久久久久久久久久 | 久久伊 | 一级生活毛片 | 亚洲作爱视频 | 一区二区三区在线播放视频 | 高清中文字幕在线a片 | 娇小萝被两个黑人用半米长 | 日韩av资源 | 在线观看视频日韩 | 成人在线小视频 | 精品国产精品三级精品av网址 | 欧美成人性生活片 | 岛国精品 | 91精品国产入口在线 | 91波多野结衣 | 高清在线一区二区三区 | 女女同性女同一区二区三区按摩 | 日韩电影中文字幕在线观看 | 三级黄色在线播放 | 婷婷午夜精品久久久久久性色av | 天天在线免费视频 | 国产又粗又黄又爽 | 色老头在线视频 | 亚洲成av人片在线观看无 | 操老女人视频 | 一级片在线| 国模吧一区二区三区 | 国产精品丝袜视频无码一区69 | 免费视频二区 | 亚洲欧美激情另类校园 | 女厕厕露p撒尿八个少妇 | 日韩在线播放一区 | 日韩精品观看 | 成为性瘾网黄的yy对象后 | 国产又粗又黄的视频 | 我和我的太阳泰剧在线观看泰剧 | 久久久久高潮 | 麻豆porn| 国产精品无码一本二本三本色 | 久久久久亚洲av成人网人人网站 | 五月天婷婷激情网 | 台湾佬中文字幕 | av黄网站 | 日本欧美国产在线 | 久操视频在线观看 | 天堂在线资源8 | 日韩视频区 | 一卡二卡三卡 | av在线综合网 | 夜晚福利 | 暖暖av在线 |