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

首頁 > 數據庫 > MySQL > 正文

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

2024-07-24 12:39:53
字體:
來源:轉載
供稿:網友

朋友在使用mysql時提示”not exists(b except A)”錯誤了,下文章小編整理了一篇此錯誤問題的解決辦法,數據庫系統概論第六版中文版中的51頁,有個"not exists(b except A)" 的例子,要求查詢“找出選修了 Biology 系開設的所有課程的學生”,實驗平臺搭建去我博客搜索,書上的sql 命令如下:

  1. select S.ID , S.name 
  2. from student as S 
  3. where not exists (( select course_id 
  4. from course 
  5. where dept_name = 'Biology'
  6. except 
  7. select T.course_id 
  8. from takes as T 
  9. where S.ID = T.ID )); 

這個在sql server上運行是沒有問題的,但是如果在myql下運行就是如下報錯:

  1. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
  2. corresponds to your MySQL server version for the right syntax to use near 'excep 
  3. select T.course_id 
  4. from takes as T 
  5. where S.ID = T.ID ))' at line 6 
  6. mysql> 

因為mysql下不支持 except的命令,所以,我們要換個方式來查詢“找出選修了 Biology 系開設的所有課程的學生”.

其實,not exists(B except A)和 not in 差不多的,所以,我們可以使用下面的sql命令達到查詢要求,先看下student表中的記錄:

  1. mysql> select * from student; 
  2. +-------+----------+------------+----------+ 
  3. | ID | name | dept_name | tot_cred | 
  4. +-------+----------+------------+----------+ 
  5. | 00128 | Zhang | Comp. Sci. | 102 | 
  6. | 12345 | Shankar | Comp. Sci. | 32 | 
  7. | 19991 | Brandt | History | 80 | --Vevb.com 
  8. | 23121 | Chavez | Finance | 110 | 
  9. | 44553 | Peltier | Physics | 56 | 
  10. | 45678 | Levy | Physics | 46 | 
  11. | 54321 | Williams | Comp. Sci. | 54 | 
  12. | 55739 | Sanchez | Music | 38 | 
  13. | 70557 | Snow | Physics | 0 | 
  14. | 76543 | Brown | Comp. Sci. | 58 | 
  15. | 76653 | Aoi | Elec. Eng. | 60 | 
  16. | 98765 | Bourikas | Elec. Eng. | 98 | 
  17. | 98988 | Tanaka | Biology | 120 | 
  18. +-------+----------+------------+----------+ 
  19. 13 rows in set (0.00 sec) 

takes表中的記錄:

  1. mysql> select * from takes; 
  2. +-------+-----------+--------+----------+------+-------+ 
  3. | ID | course_id | sec_id | semester | year | grade | 
  4. +-------+-----------+--------+----------+------+-------+ 
  5. | 00128 | CS-101 | 1 | Fall | 2009 | A | 
  6. | 00128 | CS-347 | 1 | Fall | 2009 | A- | 
  7. | 12345 | CS-101 | 1 | Fall | 2009 | C | 
  8. | 12345 | CS-190 | 2 | Spring | 2009 | A | 
  9. | 12345 | CS-315 | 1 | Spring | 2010 | A | 
  10. | 12345 | CS-347 | 1 | Fall | 2009 | A | 
  11. | 19991 | HIS-351 | 1 | Spring | 2010 | B | 
  12. | 23121 | FIN-201 | 1 | Spring | 2010 | C+ | 
  13. | 44553 | PHY-101 | 1 | Fall | 2009 | B- | 
  14. | 45678 | CS-101 | 1 | Fall | 2009 | F | 
  15. | 45678 | CS-101 | 1 | Spring | 2010 | B+ | 
  16. | 45678 | CS-319 | 1 | Spring | 2010 | B | 
  17. | 54321 | CS-101 | 1 | Fall | 2009 | A- | 
  18. | 54321 | CS-190 | 2 | Spring | 2009 | B+ | 
  19. | 55739 | MU-199 | 1 | Spring | 2010 | A- | 
  20. | 76543 | CS-101 | 1 | Fall | 2009 | A | 
  21. | 76543 | CS-319 | 2 | Spring | 2010 | A | 
  22. | 76653 | EE-181 | 1 | Spring | 2009 | C | 
  23. | 98765 | CS-101 | 1 | Fall | 2009 | C- | 
  24. | 98765 | CS-315 | 1 | Spring | 2010 | B | 
  25. | 98988 | BIO-101 | 1 | Summer | 2009 | A | 
  26. | 98988 | BIO-301 | 1 | Summer | 2010 | NULL | 
  27. +-------+-----------+--------+----------+------+-------+ 
  28. 22 rows in set (0.00 sec) 

course表中的記錄:

  1. mysql> select * from course; 
  2. +-----------+----------------------------+------------+---------+ 
  3. | course_id | title | dept_name | credits | 
  4. +-----------+----------------------------+------------+---------+ 
  5. | BIO-101 | Intro. to Biology | Biology | 4 | 
  6. | BIO-301 | Genetics | Biology | 4 | 
  7. | BIO-399 | Computational Biology | Biology | 3 | 
  8. | CS-101 | Intro. to Computer Science | Comp. Sci. | 4 | 
  9. | CS-190 | Game Design | Comp. Sci. | 4 | 
  10. | CS-315 | Robotics | Comp. Sci. | 3 | 
  11. | CS-319 | Image Processing | Comp. Sci. | 3 | 
  12. | CS-347 | Database System Concepts | Comp. Sci. | 3 | 
  13. | EE-181 | Intro. to Digital Systems | Elec. Eng. | 3 | 
  14. | FIN-201 | Investment Banking | Finance | 3 | 
  15. | HIS-351 | World History | History | 3 | 
  16. | MU-199 | Music Video Production | Music | 3 | 
  17. | PHY-101 | Physical Principles | Physics | 4 | 
  18. +-----------+----------------------------+------------+---------+ 
  19. 13 rows in set (0.00 sec) 

接著看一下'Biology'系總共開了哪些課程:

  1. mysql> select course_id 
  2. -> from course 
  3. -> where dept_name = 'Biology'
  4. +-----------+ 
  5. | course_id | 
  6. +-----------+ 
  7. | BIO-101 | 
  8. | BIO-301 | 
  9. | BIO-399 | 
  10. +-----------+ 
  11. rows in set (0.00 sec) 

通過觀察,我們的都能輕易看出,“找出選修了 Biology 系開設的所有課程的學生”的結果是,就只有一個叫Tanaka 上了Biology系開的課程.

所以,我們可以將書上的改成except命令改成:

  1. select distinct S.ID , S.name 
  2. from student as S ,takes as T 
  3. where S.ID = T.ID and course_id in ( 
  4. select course_id 
  5. from course 
  6. where dept_name = 'Biology'); 
  7. --查詢結果: 
  8. +-------+--------+ 
  9. | ID | name | 
  10. +-------+--------+ 
  11. | 98988 | Tanaka | 
  12. +-------+--------+ 
  13. 1 row in set (0.03 sec) 

我們將問題改成“找出選修了 Comp. Sci,系開設的所有課程的學生” ,執行:

  1. select distinct S.ID , S.name 
  2. from student as S ,takes as T 
  3. where S.ID = T.ID and course_id in ( 
  4. select course_id 
  5. from course 
  6. where dept_name = 'Comp. Sci.'); 
  7. --查詢結果: 
  8. +-------+----------+ 
  9. | ID | name | 
  10. +-------+----------+ 
  11. | 00128 | Zhang | 
  12. | 12345 | Shankar | 
  13. | 45678 | Levy | 
  14. | 54321 | Williams | 
  15. | 76543 | Brown | 
  16. | 98765 | Bourikas | 
  17. +-------+----------+ 
  18. rows in set (0.00 sec)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涡阳县| 彰武县| 惠来县| 屏山县| 屯昌县| 古田县| 察隅县| 东海县| 洞头县| 清远市| 西盟| 诏安县| 内丘县| 常熟市| 闽侯县| 威海市| 牡丹江市| 古田县| 航空| 区。| 黄平县| 昌图县| 桦南县| 雅江县| 永顺县| 镇康县| 宁晋县| 富锦市| 青岛市| 衡阳县| 景洪市| 于都县| 茶陵县| 五指山市| 珠海市| 永清县| 达拉特旗| 平凉市| 广东省| 仁布县| 五常市|