資料庫程式設計 – PHP+MySQL 第七章 巢狀查詢程式  上一頁      下一頁

 

7-5 多重查詢 Exists 連結

內容:

  • 7-5-1 Exists 連結語法

  • 7-5-2 範例研討:查詢已開課教師

  • 7-5-3 範例研討:查詢未選課學生

7-5-1 Exists 連結語法

只要子查詢語句傳回來不是空值的話,表示條件成立,而且上層查詢語句的 Where 條件敘述,與子查詢語句的Where 條件相同,基本語句如下:

Select 欄位

From 表格

Where exists (select 欄位 

            From 表格 

            Where 條件敘述);

或條件不存在(not exists),基本語法如下:

Select 欄位

From 表格

Where not exists (select 欄位 

               From 表格 

               Where 條件敘述);

7-5-2 範例研討:查詢已開課教師

(A) 程式功能:Ex7_10

 請製作一網頁,可查詢各系這學期已經排課的教師代碼與姓名,查詢網頁下:

查詢結果的網頁如下:

(B) SQL 製作技巧

全校所有開課課程登錄於 open_course 表內,由 teacher_ID 表示哪一位老師開這門課,我們由 department 查出系別代碼,再由 teachers 查出該系教師名冊,如果這些老師已存在 open_course 表內 (利用 Exists 連結),則表示他們已經排課,則印出它的教師編碼與姓名。其中比較特殊的是在 open_course 查詢中,條件須 teacher_ID = teachers.teacher_ID,表示是 teachers 表內的 teacher_ID 比較是否相符。SQL語法如下: (假設查詢『資訊管理系』。

select  teacher_ID, name

from teachers

where exists (select *

           from open_course

           where teacher_ID = teachers.teacher_ID)

and dep_ID = (select Dep_ID

            from department

             where dep_name = "資訊管理系");

(C) 網頁製作技巧

『表單網頁』製作下拉式選單由 department 表查詢出 Dep_ID,傳送給『執行網頁』,由它利用Exists 連結open_course查詢出結果,如下圖所示:

(D) 表單網頁:Ex7_10-form.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

        $select_db = mysqli_connect("localhost", "root", "12345678", "course_db")

                or die("MySQL 伺服器連結失敗 <br>");

        // 查詢系所編碼

        $sql_query = "select Dep_ID, dep_name

                  from department";

        $result = mysqli_query($select_db, $sql_query);

   echo "<p><font size='3'> 查詢系所已開課教師名單 </font></p>";

   echo "<form name='表單' method='post' action='Ex7_10-action.php'>";

        echo "選擇系別名稱:";

        echo "<select name='Dep_ID'>";

        while($row=mysqli_fetch_row($result)) {

            echo "<option value='$row[0]'> $row[1]($row[0])</option><BR>";

            }

        echo "</select>";

        echo "<BR><BR>";

                echo "<input type='submit' value='送出'>";

        echo "</form>";

    mysql_close($db_link);

?>

(E) 接收與處理網頁:Ex7_10-action.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<?php

        $Dep_ID = $_POST["Dep_ID"];       

        $select_db = mysqli_connect("localhost", "root", "12345678", "course_db")

                or die("MySQL 伺服器連結失敗 <br>");

        // 查詢系所開課課程

    $sql_query = "select teacher_ID, name

                  from teachers

                  where exists (select *

                             from open_course

                             where teacher_ID = teachers.teacher_ID)

                                 and dep_ID = (select Dep_ID

                                             from department

                                                        where Dep_ID = '$Dep_ID')";

       

        $result = mysqli_query($select_db, $sql_query);

        $items = array("教師編碼","教師姓名");

        echo "<table border = '2'><tr align='center'>";

        // 利用 foreach 迴圈印出陣列

        foreach ($items as $value) {

                echo "<td> $value </td>";

        }

        echo "</tr>";

        while($row=mysqli_fetch_row($result)) {

                echo "<tr>";

                for($j=0; $j<mysqli_num_fields($result); $j++) {

                        echo "<td>$row[$j]</td>";

                }

                echo "</tr>";

        }

        echo "</table>";

        mysql_close($db_link);

?>

7-5-3範例研討:查詢未選課學生

(A) 程式功能:Ex7_11

依系所名稱查詢這學期該系尚未選課學生的學號與姓名,『選單網頁』如下:

『執行網頁』查詢結果如下:

(B) SQL 製作技巧

由上圖可以看出,先查詢該系所有學生的 student_ID ( departmentclass_ID students ),只要不存在於 select_course 表內,擇表示該學生還未選課,SQL語法如下: (假設查詢『資訊管理系』。

select student_ID, name

from students

where not exists (select *

              from select_course

              where student_ID = students.student_ID)

and class_ID in (select class_ID

              from classes

              where dep_ID = (select Dep_ID

                            from department

                             where dep_name = "資訊管理系"));

(C) 網頁製作技巧

『表單網頁』製作下拉式選單由 department 表查詢出 Dep_ID,傳送給『執行網頁』,由它利用Not Exists 連結classesstudentsselect_course查詢出結果,如下圖所示:

(D) 表單網頁:Ex7_11-form.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?php

        $select_db = mysqli_connect("localhost", "root", "12345678", "course_db")

                or die("MySQL 伺服器連結失敗 <br>");

        // 查詢系所編碼

        $sql_query = "select Dep_ID, dep_name

                  from department";

        $result = mysqli_query($select_db, $sql_query);

   echo "<p><font size='3'> 查詢系所還未選課學生名單 </font></p>";

   echo "<form name='表單' method='post' action='Ex7_11-action.php'>";

        echo "選擇系別名稱:";

        echo "<select name='Dep_ID'>";

        while($row=mysqli_fetch_row($result)) {

            echo "<option value='$row[0]'> $row[1]($row[0])</option><BR>";

            }

        echo "</select>";

        echo "<BR><BR>";

                echo "<input type='submit' value='送出'>";

        echo "</form>";

    mysql_close($db_link);

?>

 (E) 接收與處理網頁:Ex7_11-action.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<?php

        $Dep_ID = $_POST["Dep_ID"];

        $select_db = mysqli_connect("localhost", "root", "12345678", "course_db")

                or die("MySQL 伺服器連結失敗 <br>");

        // 查詢系所內為選課學生

    $sql_query = "select student_ID, name

                  from students

                  where not exists (select *

                                    from select_course

                                    where student_ID = students.student_ID)

                                  and class_ID in (select class_ID

                                                  from classes

                                                                  where Dep_ID = '$Dep_ID')";

       

        $result = mysqli_query($select_db, $sql_query);

        $items = array("學生學號","學生姓名");

        echo "<table border = '2'><tr align='center'>";

        // 利用 foreach 迴圈印出陣列

        foreach ($items as $value) {

                echo "<td> $value </td>";

        }

        echo "</tr>";

        while($row=mysqli_fetch_row($result)) {

                echo "<tr>";

                for($j=0; $j<mysqli_num_fields($result); $j++) {

                        echo "<td>$row[$j]</td>";

                }

                echo "</tr>";

        }

        echo "</table>";

        mysql_close($db_link);

?>

翻轉工作室:粘添壽

course_db 關聯圖

 

資料庫程式設計:

 

 

 

翻轉電子書系列: