多表连接查询

  • 内连接查询
    连接结果默认是迪卡尔积, 根据连接条件连接满足条件的记录
  1. select t1.feild,...t2.field,... from tableName1 as t1 inner join tableName2 as t2 on condition;
  • 左外连接查询
    连接结果以左表为准, 如果在右表中没有没有满足条件的记录,则左表记录后连接 NULL 值
  1. select t1.feild,...t2.field,... from tableName1 as t1 left join tableName2 as t2 on condition;
  • 右外连接查询
    连接结果以右表为准, 如果在左表中没有没有满足条件的记录,则右表记录后连接 NULL 值
    **可以通过
  1. select t1.feild,...t2.field,... from tableName1 as t1 right join tableName2 as t2 on condition;

两表以上多表连接查询

  1. select t1.feild,...t2.field,t3.field,... from tableName1 as t1 xxx join tableName2 as t2 on condition xxx join tableName3 as t3 on condition....,....;

select t1.feild,…t2.field,t3.field,… from tableName1 as t1 xxx join tableName2 as t2 on condition xxx join tableName3 as t3 on condition….,….;

  • 多表连接查询三-第五题(专题3-5)

    1. SELECT class.caption, student.`name`,course.`name` AS subject,score.number
    2. FROM class
    3. LEFT JOIN student ON class.id = student.classId -- 找到学生信息
    4. LEFT JOIN classandcourse ON classandcourse.classId = class.id -- 找到课程编号
    5. LEFT JOIN course ON classandcourse.courseId = course.id -- 找到课程信息
    6. LEFT JOIN score ON score.studentId = student.id AND score.courseId = course.id
    7. WHERE class.caption = '一年一班'
  • 使用表别名

    1. select t1.caption,t2.name, t4.name,t5.number from class t1
    2. left join student t2 on t1.id = t2.classId
    3. left join classandcourse t3 on t2.classId = t3.classID
    4. left join course t4 on t3.courseId = t4.id
    5. left join score t5 on t2.id = t5.studentId and t4.id = t5.courseId
    6. where t1.caption='一年一班';
  • 扩展操作

    1. select t1.caption,t2.name, t4.name,t5.number from class t1
    2. left join student t2 on t1.id = t2.classId
    3. left join classandcourse t3 on t2.classId = t3.classID
    4. left join course t4 on t3.courseId = t4.id
    5. left join score t5 on t2.id = t5.studentId and t4.id = t5.courseId
    6. where t1.caption='一年一班'
    7. order by t5.number desc
    8. limit 5

案例练习 (班级信息管理)

  1. <?php
  2. // 连接数据库
  3. $url = "mysql:host=mysql;dbname=database_lesson_30511_14_11214";
  4. $user = "lesson_30511_14_11214";
  5. $passwd = "b58c4ebfdcced037d1178d91a452015d";
  6. $pdo = new PDO($url,$user,$passwd);
  7. // 相关变量
  8. $page = 1; // 当前页码
  9. $pageSize = 3; // 当前每页显示记录数
  10. $start = 0; // 获取分页数据时的起始位置
  11. $allNum = 0; // 表中所有的记录数
  12. $allPage = 0; // 表中所有的页数
  13. // 获取所有的记录数
  14. $sql = "select class.name, count(*) from class inner join student on class.id=student.classId group by class.name;";
  15. $st = $pdo->query($sql);
  16. $res = $st->fetchAll(PDO::FETCH_ASSOC);
  17. // 保存所有的记录数
  18. $allNum = count($res);
  19. // 计算所有的页数
  20. $allPage = ceil($allNum/$pageSize);
  21. if(isset($_GET['p'])){
  22. $page = $_GET['p'];
  23. }
  24. // 通过页码云计算起始查询位置,进进行获取数据
  25. $start = ($page-1)*$pageSize;
  26. $sql = "select class.name, count(*) as num from class inner join student on class.id=student.classId group by class.name limit {$start}, {$pageSize}";
  27. $st = $pdo->query($sql);
  28. $res = $st->fetchAll(PDO::FETCH_ASSOC);
  29. // var_dump($res);
  30. ?>
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="UTF-8">
  35. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  36. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  37. <title>Document</title>
  38. <style>
  39. #content{
  40. width:800px;
  41. text-align: center;
  42. margin:0 auto;
  43. background: #ccc;
  44. font-size: 20px;
  45. }
  46. #table{
  47. width:800px;
  48. }
  49. table{
  50. border-collapse:collapse;
  51. margin: 0 auto;
  52. }
  53. table,td,th{
  54. border: 1px solid red;
  55. }
  56. tr:nth-child(1){
  57. background: cyan;
  58. }
  59. td{
  60. text-align: center;
  61. width:400px;
  62. }
  63. #ctl{
  64. width:800px;
  65. margin:10px 0;
  66. }
  67. #btn{
  68. width:700px;
  69. list-style: none;
  70. margin: 0 auto;
  71. display: flex;
  72. padding: 0;
  73. justify-content: space-between;
  74. }
  75. li{
  76. width:100px;
  77. height: 25px;
  78. border-radius:5px;
  79. border:1px solid blue;
  80. margin:0 ;
  81. }
  82. .num{
  83. width:25px;
  84. height: 25px;
  85. border:1px solid blue;
  86. border-radius:5px;
  87. margin: 0 -25px;
  88. }
  89. a{
  90. text-decoration: none;
  91. }
  92. #info{
  93. text-align: center;
  94. font-size: 18px;
  95. }
  96. span{
  97. margin-left:20px;
  98. }
  99. </style>
  100. </head>
  101. <body>
  102. <div id="content">
  103. <div id='title'>
  104. <h1>班级信息管理</h1>
  105. </div>
  106. <div id='table'>
  107. <table width="600" >
  108. <tr background="#ccc">
  109. <th>班级名称</th>
  110. <th>人数</th>
  111. </tr>
  112. <!--对查询数据进行显示-->
  113. <?php foreach($res as $val){ ?>
  114. <tr>
  115. <td> <?php echo $val["name"]; ?></td>
  116. <td> <?php echo $val["num"]."人"; ?></td>
  117. </tr>
  118. <?php } ?>
  119. </table>
  120. </div>
  121. <!--控制按钮-->
  122. <div id='ctl'>
  123. <ul id='btn'>
  124. <li>
  125. <!--如果是到首页,固定页码为1-->
  126. <a href="<?php echo "index.php?p=1"; ?>">
  127. 首页
  128. </a>
  129. </li>
  130. <li>
  131. <!--如果当前页面不是第一页,则可以进行页面减一,跳到上一次,否则一直为第一页-->
  132. <a href="<?php
  133. if($page != 1){
  134. echo "index.php?p=" . ($page - 1);
  135. }else{
  136. echo "index.php?p=1";
  137. }
  138. ?>">
  139. 上一页
  140. </a>
  141. </li>
  142. <?php
  143. for($i=1;$i<=$allPage;$i++){ ?>
  144. <li class="num" <?php
  145. if($page == $i){ ?>
  146. style = "background: red;";
  147. <?php } ?>
  148. >
  149. <a href="<?php echo "index.php?p=$i"; ?>">
  150. <?php echo $i; ?>
  151. </a>
  152. </li>
  153. <?php } ?>
  154. </li>
  155. <li>
  156. <!--如果当前页面不是第一页,则可以进行页面加一,跳到下一次,否则一直为末页-->
  157. <a href="<?php
  158. if($page != $allPage){
  159. echo "index.php?p=" . ($page + 1);
  160. }else{
  161. echo "index.php?p=" . $allPage;
  162. }
  163. ?>">
  164. 下一页
  165. </a>
  166. </li>
  167. <li>
  168. <!--固定页码为最后一页-->
  169. <a href="<?php echo "index.php?p=" . $allPage;?>">
  170. 末页
  171. </a>
  172. </li>
  173. </ul>
  174. </div>
  175. <!--显示记录信息-->
  176. <div id="info">
  177. <?php echo "<span>当前页: {$page}</span> <span>总页数: {$allPage}</span> <span>总数据量:{$allNum}</span>"; ?>
  178. </div>
  179. </div>
  180. </body>
  181. </html>