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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250420.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250420.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250420.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

day11-元組與字典

1、元組Tuple與列表類似&#xff0c;不同之處在于元組的元素不能修改。 元組使用小括號&#xff0c;列表使用中括號。元組可以查詢&#xff0c;可以使用內置函數count、index。但是不能修改、增加、刪除&#xff08;兒子不能&#xff0c;孫子有可能&#xff09;。name (a,a,b)…

算法 --- [map的使用]求最大和諧子序列

說明 和諧數組是指一個數組里元素的最大值和最小值之間的差別正好是1。 現在&#xff0c;給定一個整數數組&#xff0c;你需要在所有可能的子序列中找到最長的和諧子序列的長度。 輸入: [1,3,2,2,5,2,3,7] 輸出: 5 原因: 最長的和諧數組是&#xff1a;[3,2,2,2,3]. 思路 創…

vue問題四:富文本編輯器上傳圖片

vue使用富文本編輯器上傳圖片&#xff1a; 我是用的是wangEditor 富文本編輯器 demo:http://www.wangeditor.com/ 1).安裝依賴:npm install wangeditor 2).我自己是創建了一個組件這樣再用到的時候可以直接調用&#xff08;可能有更簡單的方法&#xff09; <template lang&q…

R 實用命令 1

Quit and restart a clean R session from within R? If youre in RStudio: command/ctrl shift F10 .rs.restartR()轉載于:https://www.cnblogs.com/shuaihe/p/8945039.html

vscode --- 快捷鍵格式化代碼時,分號消失

問題復現 最近在vscode中,格式化代碼(快捷鍵 alt shift F)時,分號會莫名奇妙的消失 對于習慣打分號的我來說,看起來很別扭… 解決方案. 我使用的是prettier這個插件來設置格式化的.安裝方法如下: 點擊左側的: 搜索 prettier, 選擇 Prettier - Code formatter 安裝好了之后…

【python開發】構造一個可以查看,填加和返回的字典

當我們在面對一個字典的時候&#xff0c;基本功能有查找&#xff0c;填加&#xff0c;和返回上一級&#xff0c;我們利用上一篇的字典&#xff0c;寫了一個可以實現字典基本功能的小程序&#xff1a; #!/usr/bin/env python # -*- coding:utf-8 -*- dp {亞洲:{中國:{山東:{},北…

算法 --- [隊列結構]二叉樹的層次遍歷

思路 使用隊列: 初始化的時候,將root, push進隊列q中循環隊列q,當其中不為空時,取出第一個元素(q.shift),記為r若r.left不為空,將r.left推進q,若r.right不為空,將r.right推進q 記錄層次: 4. 初始化設置i 0; 5. 在入隊的時候,入隊一個對象{r: root, i} 6. 出隊時,使用es6的解…

Redis在windows下安裝過程(轉載)

轉載自&#xff08;http://www.cnblogs.com/M-LittleBird/p/5902850.html&#xff09; 一、下載windows版本的Redis 官網以及沒有下載地址&#xff0c;只能在github上下載&#xff0c;官網只提供linux版本的下載 官網下載地址&#xff1a;http://redis.io/download github下載地…

C# Socket網絡編程精華篇

我們在講解Socket編程前&#xff0c;先看幾個和Socket編程緊密相關的概念&#xff1a; TCP/IP層次模型當然這里我們只討論重要的四層 01&#xff0c;應用層(Application)&#xff1a;應用層是個很廣泛的概念&#xff0c;有一些基本相同的系統級TCP/IP應用以及應用協議&#xff…

Luogu1443 馬的遍歷【STL通俗BFS】

喜聞樂見當做BFS的STL模板做了 qwq我這樣的蒟蒻也就只能發發模板題 #include<cstdio> #include<cstring> #include<cmath> #include<queue> using namespace std; struct xy{int x,y; }node,top; int dx[8]{1,1,2,2,-1,-1,-2,-2}; int dy[8]{2,-2,1,-1…

javascript --- [虛擬DOM] 初始化 實現

說明 本篇主要說明為什么要使用虛擬DOM技術,以及如何實現簡單的虛擬dom您將會學到: 1.原生JS對DOM的操作 2.虛擬DOM的相關概念 3.DIFF算法的基礎概念 為什么提出 -> DOM操作慢 我們使用createElement屬性來創建一個最常見的div,看看一個最常見的DOM有多少個屬性 <scri…

模塊單元學習筆記(日志記錄模塊os模塊sys)

一、日志記錄模塊 Logging 默認情況下&#xff0c;logging將日志打印到屏幕&#xff0c;日志級別大小關系為&#xff1a;CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET&#xff0c;當然也可以自己定義日志級別。 DEBUG&#xff1a;詳細的信息,通常只出現…

webpack --- [4.x]你能看懂的webpack項目初始化

說明: 本篇文章主要做如下事情: 創建一個基本的webpack4.x 項目[報錯]: The mode option has not been set, webpack will fallback to production for this value[報錯]: ERROR in Entry module not found: Error: Can not resolve ./src in D:\L-react\HeiMa\01.webpack-ba…

tomcat8 進入不了Manager App 界面 403 Access Denied

準備 1.注釋掉context.xml中的value屬性 使用下面的命令&#xff1a; vim /usr/local/tomcats/tomcat-daily/webapps/manager/META-INF/context.xml 注釋掉其中value節點 2.修改tomcat-users.xml文件 加入下面的配置 <role rolename"manager-gui" /><role …

SCRIPT70: 沒有權限

主要原因&#xff1a;iframe安全而引發的問題&#xff0c;瀏覽器中js是沒有垮域訪問的權限的。如果用到iframe首先確保不垮域&#xff0c;或者不用iframe以繞開這個問題。 另外在jquery的早期版本中如&#xff1a;jquery-1.9.1.js $(#iframeWeb).attr(src, url);出現這樣的問題…

webpack --- 在項目中使用React

說明: 分為2步: 首先導入react 和 react-dom:保證了虛擬DOM的創建和使用使用babel轉碼器: 由于DOM結構太多,每次使用React.createElement創建虛擬DOM會給開發帶來很大壓力,因此采用html的寫法,通過babel轉碼器轉換成React語法,可以很大程度上提高開發效率 項目源代碼 在項目…

js改變select下拉框默認選擇的option

比較簡單&#xff0c;記錄一下 var obj document.getElementById("fun"); obj.options[0].selected true; 轉載于:https://www.cnblogs.com/vicF/p/9844028.html

vue攔截器實現統一token,并兼容IE9驗證

項目中使用vue搭建前端頁面&#xff0c;并通過axios請求后臺api接口&#xff0c;完成數據交互。如果驗證口令token寫在在每次的接口中&#xff0c;也是個不小的體力活&#xff0c;而且也不靈活。這里分享使用vue自帶攔截器&#xff0c;給每次請求的頭部添加token&#xff0c;而…

Android Studio --- [學習筆記]Button、TextView、EditText

說明 源代碼為了更全面的了解RN,先熟悉一下Android開發 第1章 Android 初體驗 1.1 Android開發概述 Android是Google開發的操作系統Android開發是移動應用開發的表現形式之一(Android、IOS、H5 App、Native H5、 RN、ionic、MUI…) 1.2 Android開發工具 Android Studio為…

BZOJ2154: Crash的數字表格 BZOJ2693: jzptab

【傳送門&#xff1a;BZOJ2154&BZOJ2693】 簡要題意&#xff1a; 給出n,m&#xff0c;求$\sum_{i1}^{n}\sum_{j1}^{m}LCM(i,j)$ 題解&#xff1a; 莫比烏斯反演&#xff08;因為BZOJ2693是多組數據&#xff0c;數據強一點&#xff0c;所以代碼用BZOJ2693的&#xff09; 設n…