多表连接查询
- 内连接查询
连接结果默认是迪卡尔积, 根据连接条件连接满足条件的记录
select t1.feild,...t2.field,... from tableName1 as t1 inner join tableName2 as t2 on condition;
- 左外连接查询
连接结果以左表为准, 如果在右表中没有没有满足条件的记录,则左表记录后连接 NULL 值
select t1.feild,...t2.field,... from tableName1 as t1 left join tableName2 as t2 on condition;
- 右外连接查询
连接结果以右表为准, 如果在左表中没有没有满足条件的记录,则右表记录后连接 NULL 值
**可以通过
select t1.feild,...t2.field,... from tableName1 as t1 right join tableName2 as t2 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)
SELECT class.caption, student.`name`,course.`name` AS subject,score.numberFROM classLEFT JOIN student ON class.id = student.classId -- 找到学生信息LEFT JOIN classandcourse ON classandcourse.classId = class.id -- 找到课程编号LEFT JOIN course ON classandcourse.courseId = course.id -- 找到课程信息LEFT JOIN score ON score.studentId = student.id AND score.courseId = course.idWHERE class.caption = '一年一班'
使用表别名
select t1.caption,t2.name, t4.name,t5.number from class t1left join student t2 on t1.id = t2.classIdleft join classandcourse t3 on t2.classId = t3.classIDleft join course t4 on t3.courseId = t4.idleft join score t5 on t2.id = t5.studentId and t4.id = t5.courseIdwhere t1.caption='一年一班';
扩展操作
select t1.caption,t2.name, t4.name,t5.number from class t1left join student t2 on t1.id = t2.classIdleft join classandcourse t3 on t2.classId = t3.classIDleft join course t4 on t3.courseId = t4.idleft join score t5 on t2.id = t5.studentId and t4.id = t5.courseIdwhere t1.caption='一年一班'order by t5.number desclimit 5
案例练习 (班级信息管理)
<?php// 连接数据库$url = "mysql:host=mysql;dbname=database_lesson_30511_14_11214";$user = "lesson_30511_14_11214";$passwd = "b58c4ebfdcced037d1178d91a452015d";$pdo = new PDO($url,$user,$passwd);// 相关变量$page = 1; // 当前页码$pageSize = 3; // 当前每页显示记录数$start = 0; // 获取分页数据时的起始位置$allNum = 0; // 表中所有的记录数$allPage = 0; // 表中所有的页数// 获取所有的记录数$sql = "select class.name, count(*) from class inner join student on class.id=student.classId group by class.name;";$st = $pdo->query($sql);$res = $st->fetchAll(PDO::FETCH_ASSOC);// 保存所有的记录数$allNum = count($res);// 计算所有的页数$allPage = ceil($allNum/$pageSize);if(isset($_GET['p'])){$page = $_GET['p'];}// 通过页码云计算起始查询位置,进进行获取数据$start = ($page-1)*$pageSize;$sql = "select class.name, count(*) as num from class inner join student on class.id=student.classId group by class.name limit {$start}, {$pageSize}";$st = $pdo->query($sql);$res = $st->fetchAll(PDO::FETCH_ASSOC);// var_dump($res);?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>#content{width:800px;text-align: center;margin:0 auto;background: #ccc;font-size: 20px;}#table{width:800px;}table{border-collapse:collapse;margin: 0 auto;}table,td,th{border: 1px solid red;}tr:nth-child(1){background: cyan;}td{text-align: center;width:400px;}#ctl{width:800px;margin:10px 0;}#btn{width:700px;list-style: none;margin: 0 auto;display: flex;padding: 0;justify-content: space-between;}li{width:100px;height: 25px;border-radius:5px;border:1px solid blue;margin:0 ;}.num{width:25px;height: 25px;border:1px solid blue;border-radius:5px;margin: 0 -25px;}a{text-decoration: none;}#info{text-align: center;font-size: 18px;}span{margin-left:20px;}</style></head><body><div id="content"><div id='title'><h1>班级信息管理</h1></div><div id='table'><table width="600" ><tr background="#ccc"><th>班级名称</th><th>人数</th></tr><!--对查询数据进行显示--><?php foreach($res as $val){ ?><tr><td> <?php echo $val["name"]; ?></td><td> <?php echo $val["num"]."人"; ?></td></tr><?php } ?></table></div><!--控制按钮--><div id='ctl'><ul id='btn'><li><!--如果是到首页,固定页码为1--><a href="<?php echo "index.php?p=1"; ?>">首页</a></li><li><!--如果当前页面不是第一页,则可以进行页面减一,跳到上一次,否则一直为第一页--><a href="<?phpif($page != 1){echo "index.php?p=" . ($page - 1);}else{echo "index.php?p=1";}?>">上一页</a></li><?phpfor($i=1;$i<=$allPage;$i++){ ?><li class="num" <?phpif($page == $i){ ?>style = "background: red;";<?php } ?>><a href="<?php echo "index.php?p=$i"; ?>"><?php echo $i; ?></a></li><?php } ?></li><li><!--如果当前页面不是第一页,则可以进行页面加一,跳到下一次,否则一直为末页--><a href="<?phpif($page != $allPage){echo "index.php?p=" . ($page + 1);}else{echo "index.php?p=" . $allPage;}?>">下一页</a></li><li><!--固定页码为最后一页--><a href="<?php echo "index.php?p=" . $allPage;?>">末页</a></li></ul></div><!--显示记录信息--><div id="info"><?php echo "<span>当前页: {$page}</span> <span>总页数: {$allPage}</span> <span>总数据量:{$allNum}</span>"; ?></div></div></body></html>
