国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql下的not exists b except A解決辦法

2024-07-24 12:35:05
字體:
來源:轉載
供稿:網友
  朋友在使用mysql時提示”not exists(b except A)”錯誤了,下文章小編整理了一篇此錯誤問題的解決辦法,數據庫系統概論第六版中文版中的51頁,有個"not exists(b except A)" 的例子,要求查詢“找出選修了 Biology 系開設的所有課程的學生”,實驗平臺搭建去我博客搜索,書上的sql 命令如下:
 
  select S.ID , S.name
  from student as S
  where not exists (( select course_id
  from course
  where dept_name = 'Biology')
  except
  ( select T.course_id
  from takes as T
  where S.ID = T.ID ));
  這個在sql server上運行是沒有問題的,但是如果在myql下運行就是如下報錯:
 
  ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
  corresponds to your MySQL server version for the right syntax to use near 'excep
  t
  ( select T.course_id
  from takes as T
  where S.ID = T.ID ))' at line 6
  mysql>
  因為mysql下不支持 except的命令,所以,我們要換個方式來查詢“找出選修了 Biology 系開設的所有課程的學生”.
 
  其實,not exists(B except A)和 not in 差不多的,所以,我們可以使用下面的sql命令達到查詢要求,先看下student表中的記錄:
 
  mysql> select * from student;
  +-------+----------+------------+----------+
  | ID | name | dept_name | tot_cred |
  +-------+----------+------------+----------+
  | 00128 | Zhang | Comp. Sci. | 102 |
  | 12345 | Shankar | Comp. Sci. | 32 |
  | 19991 | Brandt | History | 80 | --phpfensi.com
  | 23121 | Chavez | Finance | 110 |
  | 44553 | Peltier | Physics | 56 |
  | 45678 | Levy | Physics | 46 |
  | 54321 | Williams | Comp. Sci. | 54 |
  | 55739 | Sanchez | Music | 38 |
  | 70557 | Snow | Physics | 0 |
  | 76543 | Brown | Comp. Sci. | 58 |
  | 76653 | Aoi | Elec. Eng. | 60 |
  | 98765 | Bourikas | Elec. Eng. | 98 |
  | 98988 | Tanaka | Biology | 120 |
  +-------+----------+------------+----------+
  13 rows in set (0.00 sec)
  takes表中的記錄:
 
  mysql> select * from takes;
  +-------+-----------+--------+----------+------+-------+
  | ID | course_id | sec_id | semester | year | grade |
  +-------+-----------+--------+----------+------+-------+
  | 00128 | CS-101 | 1 | Fall | 2009 | A |
  | 00128 | CS-347 | 1 | Fall | 2009 | A- |
  | 12345 | CS-101 | 1 | Fall | 2009 | C |
  | 12345 | CS-190 | 2 | Spring | 2009 | A |
  | 12345 | CS-315 | 1 | Spring | 2010 | A |
  | 12345 | CS-347 | 1 | Fall | 2009 | A |
  | 19991 | HIS-351 | 1 | Spring | 2010 | B |
  | 23121 | FIN-201 | 1 | Spring | 2010 | C+ |
  | 44553 | PHY-101 | 1 | Fall | 2009 | B- |
  | 45678 | CS-101 | 1 | Fall | 2009 | F |
  | 45678 | CS-101 | 1 | Spring | 2010 | B+ |
  | 45678 | CS-319 | 1 | Spring | 2010 | B |
  | 54321 | CS-101 | 1 | Fall | 2009 | A- |
  | 54321 | CS-190 | 2 | Spring | 2009 | B+ |
  | 55739 | MU-199 | 1 | Spring | 2010 | A- |
  | 76543 | CS-101 | 1 | Fall | 2009 | A |
  | 76543 | CS-319 | 2 | Spring | 2010 | A |
  | 76653 | EE-181 | 1 | Spring | 2009 | C |
  | 98765 | CS-101 | 1 | Fall | 2009 | C- |
  | 98765 | CS-315 | 1 | Spring | 2010 | B |
  | 98988 | BIO-101 | 1 | Summer | 2009 | A |
  | 98988 | BIO-301 | 1 | Summer | 2010 | NULL |
  +-------+-----------+--------+----------+------+-------+
  22 rows in set (0.00 sec)
  course表中的記錄:
 
  mysql> select * from course;
  +-----------+----------------------------+------------+---------+
  | course_id | title | dept_name | credits |
  +-----------+----------------------------+------------+---------+
  | BIO-101 | Intro. to Biology | Biology | 4 |
  | BIO-301 | Genetics | Biology | 4 |
  | BIO-399 | Computational Biology | Biology | 3 |
  | CS-101 | Intro. to Computer Science | Comp. Sci. | 4 |
  | CS-190 | Game Design | Comp. Sci. | 4 |
  | CS-315 | Robotics | Comp. Sci. | 3 |
  | CS-319 | Image Processing | Comp. Sci. | 3 |
  | CS-347 | Database System Concepts | Comp. Sci. | 3 |
  | EE-181 | Intro. to Digital Systems | Elec. Eng. | 3 |
  | FIN-201 | Investment Banking | Finance | 3 |
  | HIS-351 | World History | History | 3 |
  | MU-199 | Music Video Production | Music | 3 |
  | PHY-101 | Physical Principles | Physics | 4 |
  +-----------+----------------------------+------------+---------+
  13 rows in set (0.00 sec)
  接著看一下'Biology'系總共開了哪些課程:
 
  mysql> select course_id
  -> from course
  -> where dept_name = 'Biology';
  +-----------+
  | course_id |
  +-----------+
  | BIO-101 |
  | BIO-301 |
  | BIO-399 |
  +-----------+
  3 rows in set (0.00 sec)
  通過觀察,我們的都能輕易看出,“找出選修了 Biology 系開設的所有課程的學生”的結果是,就只有一個叫Tanaka 上了Biology系開的課程.
 
  所以,我們可以將書上的改成except命令改成:
 
  select distinct S.ID , S.name
  from student as S ,takes as T
  where S.ID = T.ID and course_id in (
  select course_id
  from course
  where dept_name = 'Biology');
  --查詢結果:
  +-------+--------+
  | ID | name |
  +-------+--------+
  | 98988 | Tanaka |
  +-------+--------+
  1 row in set (0.03 sec)
  我們將問題改成“找出選修了 Comp. Sci,系開設的所有課程的學生” ,執行:
 
  select distinct S.ID , S.name
  from student as S ,takes as T
  where S.ID = T.ID and course_id in (
  select course_id
  from course
  where dept_name = 'Comp. Sci.');
  --查詢結果:
  +-------+----------+
  | ID | name |
  +-------+----------+
  | 00128 | Zhang |
  | 12345 | Shankar |
  | 45678 | Levy |
  | 54321 | Williams |
  | 76543 | Brown |
  | 98765 | Bourikas |
  +-------+----------+
  6 rows in set (0.00 sec)。
 

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南丰县| 革吉县| 西平县| 年辖:市辖区| 山西省| 曲靖市| 浮山县| 津南区| 阳新县| 元阳县| 屯昌县| 平乐县| 乡城县| 临沧市| 都匀市| 双峰县| 荥经县| 肥东县| 青河县| 凭祥市| 汽车| 九江市| 渭源县| 奉贤区| 衡阳县| 会东县| 右玉县| 调兵山市| 凌云县| 龙海市| 紫金县| 班戈县| 虞城县| 浏阳市| 海门市| 绥芬河市| 花垣县| 罗城| 邹平县| 中牟县| 饶河县|