SQL資料查詢查詢選擇了同一門課程的兩個不同學生的學號及

2021-04-14 11:16:42 字數 4265 閱讀 8141

1樓:吳

你好像寫來錯了吧自,,,

我感覺應該是

select sno

from sc

where cno='1' and son in (select sno from sc where cno='2');

或者where cno='1' intersert select sno from sc where cno='2');

intersect對兩個查詢做交集。相當於and

資料庫sql語句中 查詢選修了全部課程的學生的學號和姓名 理解

2樓:匿名使用者

這思路是用了個雙重否定來求解的。因為sql中沒有全稱量詞,於是要把題目轉換成等價的存在量詞表達形式。即根據(∀x)p≡¬∃(¬p)來轉化為雙重否定的表達。

同時由於「學生x選修課程y 」

之間是不定的,需要使用兩個exist。

於是「選修了全部課程的學生」等價於「不存在(有他沒選的課的)學生」

使用了兩次not exists來實現雙重否定。

先查詢在課程裡查詢「沒有學生選的課程」,第一次否定,

然後再在學生裡查詢「沒有屬於上面情況的學生」的名字,第二次否定;

結合起來,就是 「沒有(沒選的課程)的學生」了。

好了,從裡到外寫出來,就是

select sname from student where not exists(

select * from course where not exists(

select * from sc where sno=student.sno and cno=course.cno

))這個只不過是逆向思維來解決問題的方法。舉一反三,比如要查「被全部學生都選的課程名」

則是求「不存在有學生沒選它的課程」

select cname from course where not exists(

select * from student where not exists(

select * from sc where sno=student.sno and cno=course.cno

))再如,查「所有人都沒選修的課程」,這個雖然是單次否定了,但仍需要兩個存在量詞表述。

等價於查詢「不存在有學生選了它的課程」。

select cname from course where not exists (

select * from student where exists (

select * from sc where cno=course.cno and sno=student.sno))

3樓:風嘯無名

沒有資料庫難以具體說明,總的來說,就是一個多表查詢包括學生基本資訊表、課程資訊表、成績表等,學號為主鍵,查詢姓名和課程、分數等資訊,總分用sum算。

1 。 exists 子查詢找到的提交

not exists 子查詢中 找不到的提交說明:不要去翻譯為存在和不存在,把腦袋搞暈。

2 。 建立程式迴圈的概念,這是一個動態的查詢過程。如 for迴圈 。

3 。 exists執行的流程exists首先執行外層查詢,再執行記憶體查詢,與in相反。 流程為首先取出外層中的第一元組, 再執行內層查詢,將外層表的第一元組代入,若內層查詢為真,即有結果時。

返回外層表中的第一元 組,接著取出第二元組,執行相同的演算法。一直到掃描完外層整表 。

4樓:月光雪松

樓主彆著急!

為好理解我們先從這條sql語句所要實現的功能入手。

功能:查出選修了全部課程的學資訊。那麼sql在查詢資料的時候的遍歷每一個學生資訊。判斷該學生是否滿足條件。

1 如果存在這麼一條course記錄a(暫命名為a), 則不選擇該學生。否則該學生就被查詢出來

2 那麼記錄a,是怎麼查出來的呢?a查出的條件是:不存在sc記錄b,只要不存在b,就可查出a

3 那麼b記錄是什麼?b記錄是選課資訊表,根據學號和課程號可查出記錄b

如果b為空(該學生有沒有選的課程)也就是不存在,則a就有一條記錄,根據規則2可知:因為有a,所以該學生資訊將不被輸出。

如果在sc中每一個課程編號和該學生編號為條件都能夠查出一條記錄b(也就是該學生選修了全部課程),所以a記錄不存在,則輸出該學生的資訊。

也就是在選課表中,如果學生選了全部課程(也就是滿足select * from sc where sno= student.sno and cno= course.cno)始終存在,當然,課程編號是任意的)。

那麼就輸出該學生的資訊。你不要為理解這條sql而忘記了它本身是要做什麼.

帶著sql的目的(要實現的功能)去理解就好了。

5樓:雨夜的緣分

1,select * from sc where sno= student.sno and cno= course.cno

在sc表中查詢符合sno= student.sno and cno= course.cno這兩個條件的所有資料,

2,select * from course where not exists (select * from sc where sno= student.sno and cno= course.cno);這句的意思是在course表中查詢不滿足1,中的所有資料

3,select sname from student where not exists (select * from course where not exists (select * from sc where sno= student.sno and cno= course.cno));

這整句的意思就是查詢student表中所有不滿足2,資料,就是選修了全部課程的學生了

只所以會有這麼多查詢,可能sno= student.sno and cno= course.cno這兩個條件是是sc表查詢的條件分散在另外兩個表中,引用了雙重否定,也就是肯定的意思,達到可以讓student.

sno ,course.cno,在sc表中作為條件的目的

夠詳細吧!!!!

用sql查詢同時選修了1號課和2號課的學生學號

6樓:於曉楠買甘

查詢bai

同時選修了1號和2號課的學du生學號

涉及到兩zhi個表.學生表和dao課程表

語句:select

*from

student

inner

join

scon

student.sno=sc.sno

andcno

in('1','2')

為什麼不用內where

cno=』1『

andcno=』2『

這個關係到資料容庫的優化問題,哪個執行快,就寫哪個那個語句也可以這樣寫啊where

snoin

(select

snofrom

scwhere

sno=』1『

)and

snoin(select

snofrom

scwhere

sno=』2『)

7樓:吳

你好抄像寫錯了吧,襲

,,我感覺應該是bai

select sno

from sc

where cno='1' and son in (select sno from sc where cno='2');

或者where cno='1' intersert select sno from sc where cno='2');

intersect對兩個du查詢zhi做交集。相當於daoand

8樓:envy誒

因為這樣的意思是 在一行內課號既得等於1又得等於2 並沒有這樣的

9樓:匿名使用者

sno 指的是什麼?

baicno指什麼? 上面的sql語句du是不可能zhi實現同樣的功能的。

sno如果是指dao學生學號;版

cno是指課程號的權

話。where cno=』1『 and cno=』2『是指課程號是1和2 的學生資訊。

where sno=』1『 and sno in(select sno

from sc

where sno=』2『)

是指學生學號是1和2 的學生,事實上,作為主鍵的sno沒有可能同時是1和2的。

請採納答案,支援我一下。

sql:查詢有2門以上課程是70分以下的學生的學號

SQL查詢資料語句問題,SQL查詢資料語句問題

取n到m行 1.select top m from tablename where id not in select top n id from tablename order by id asc desc 2.select top m into 臨時表 或表變數 from tablename or...

sql怎樣跨資料庫查詢,sql怎樣跨資料庫查詢oracle

使用dblink。例如 當前使用的資料庫是orcl1 要查詢的資料庫是orcl2的scott使用者的表。create public database link orcl2 scott connect to scott identified by tiger using description add...

sql語句分組查詢前10條資料,sql如何實現分組並select出每組前10個

class classid classnameproduct classid proname numselect top 10 c.classid c.classname,sum p.num from class c,product p where p.classid c.classid group...