2021-09-02

学成在线案例

下面展示一些 主要代码:

xuecheng.html文件
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>学成在线首页</title>
		<link rel="stylesheet" type="text/css" href="css/xuecheng.css"/>
	</head>
	<body>
		<!-- 头部区域 -->
		<div class="header banxin">
			<div class="logo">
				<img src="img/header.png" >
			</div>
			<div class="nav">
				<ul>
					<li><a href="#">首页</a></li>
					<li><a href="#">课程</a></li>
					<li><a href="#">职业规划</a></li>
				</ul>
			</div>
			<div class="search">
				<input type="text" name="search" id="text" value="输入关键词" />
				<button type="button"><img src="img/fa-search.png" ></button>
			</div>
			<div class="user">
				<img src="img/user.png" >
				qq-leishui
			</div>
		</div>
		<!-- 广告区域 -->
		<div class="banner">
			<div class="banxin">
				<div class="subnav">
					<ul>
						<li><a href="#">前端开发<span> > </span></a></li>
						<li><a href="#">后端开发<span> > </span></a></li>
						<li><a href="#">移动开发<span> > </span></a></li>
						<li><a href="#">人工智能<span> > </span></a></li>
						<li><a href="#">商业预测<span> > </span></a></li>
						<li><a href="#">云计算&大数据<span> > </span></a></li>
						<li><a href="#">运维&从测试<span> > </span></a></li>
						<li><a href="#">UI设计<span> > </span></a></li>
						<li><a href="#">产品<span> > </span></a></li>
					</ul>
				</div>	
					<!-- 课程表模块 -->
					<div class="course">
						<div class="coursetable">
							我的课程表
						</div>
						<div class="first">
								 继续学习 程序语言设计<br>
								 <div class="now">
								 	正在学习-使用对象
								 </div>
						</div>
						<div class="second">
							 继续学习 程序语言设计<br>
							 <div class="now">
								 正在学习-使用对象
							 </div>
						</div>
						<div class="third">
							 继续学习 程序语言设计<br>
							 <div class="now">
								 正在学习-使用对象
							 </div>
						</div>
						<div class="all">
							<button type="button"><a href="#">全部课程</a></button>
						</div>
						</div>
				</div>
		</div>
		<!-- 精品推荐 -->
		<div class="recom banxin">
			<ul>
				<li><a href="#">精品推荐</a></li>
				
				<li><a href="#">JQuery</a></li>
				
				<li><a href="#">Spark</a></li> 
				
				<li><a href="#">MySQL</a></li>
				
				<li><a href="#">JavaWeb</a></li>
				
				<li><a href="#">MySQL</a></li>
				
				<li><a href="#">JavaWeb</a></li>
				
			</ul>
			
			<span><a href="#">修改兴趣</a></span>
		</div>
		<!-- 盒子 -->
		<div class="box banxin">
			<div class="box-hd">
				<h3>精品推荐</h3>
				<a href="#">查看全部</a>
			</div>
			<div class="box-bd">
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
				<ul>
					<li>
						<img src="img/pic1.png" >
						<h4>Tink PHP 5.0 博客系统实战项目演练</h4>
						<h3>高级 <span>1125人在学习</span></h3>
					</li>
					
				</ul>
			</div>
		</div>
		<!-- 编程入门 -->
		<div class="biancheng banxin">
			<!-- 第一部分-->
			<div class="box-hd2">
				<h3>编程入门</h3>
				<div class="center">
					<span><a href="#">热门</a></span>
					<span><a href="#">初级</a></span>
					<span><a href="#">中级</a></span>
					<span><a href="#">高级</a></span>
				</div>
				<a href="#">查看全部</a>
			</div>
			<!-- 第二部分 -->
			<div class="box-bd2">
				<!-- 第二部分左边 -->
				<div class="left2">
					<img src="img/left2.png" >
				</div>
				<!-- 第二部分右边 -->
				<div class="right2">
					<!-- 右上 -->
					<div class="righttop">
						<img src="img/righttop.png" >
					</div>
					<!-- 右下 -->
					<ul>
						<li>
							<img src="img/pic11%20(2).png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li>
							<img src="img/pic11%20(2).png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li>
							<img src="img/pic11%20(2).png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li class="eight">
							<img src="img/pic11%20(2).png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<!-- 数据分析师 -->
		<div class="biancheng banxin">
			<!-- 第一部分-->
			<div class="box-hd2">
				<h3>数据分析师</h3>
				<div class="center">
					<span><a href="#">热门</a></span>
					<span><a href="#">初级</a></span>
					<span><a href="#">中级</a></span>
					<span><a href="#">高级</a></span>
				</div>
				<a href="#">查看全部</a>
			</div>
			<!-- 第二部分 -->
			<div class="box-bd2">
				<!-- 第二部分左边 -->
				<div class="left2">
					<img src="img/pic22.png" >
				</div>
				<!-- 第二部分右边 -->
				<div class="right2">
					<!-- 右上 -->
					<div class="righttop">
						<img src="img/pic23.png" >
					</div>
					<!-- 右下 -->
					<ul>
						<li>
							<img src="img/pic24.png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li>
							<img src="img/pic24.png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li>
							<img src="img/pic24.png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
					<ul>
						<li class="eight">
							<img src="img/pic24.png" >
							<h4>Android Hybrid APP开发实战 H5+原生!</h4>
							<h5>高级  <span>1125人在学习</span></h5>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<!-- 机器学习工程师 -->
		<div class="mestudy banxin">
			<!-- 上部分-->
			<div class="box-hd3">
				<h3>机器学习工程师</h3>
				<div class="center">
					<span><a href="#">热门</a></span>
					<span><a href="#">初级</a></span>
					<span><a href="#">中级</a></span>
					<span><a href="#">高级</a></span>
				</div>
				<a href="#">查看全部</a>
			<!-- 下部分 -->
			<div class="box-bd3">
				<ul>
					<li>
						<img src="img/pic31.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic31.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic31.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic31.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li class="fifth">
						<img src="img/pic31.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
			</div>
		</div>
		</div>
		<!-- 前端开发工程师 -->
		<div class="mestudy banxin">
			<!-- 上部分-->
			<div class="box-hd3">
				<h3>前端开发工程师</h3>
				<div class="center">
					<span><a href="#">热门</a></span>
					<span><a href="#">初级</a></span>
					<span><a href="#">中级</a></span>
					<span><a href="#">高级</a></span>
				</div>
				<a href="#">查看全部</a>
			<!-- 下部分 -->
			<div class="box-bd3">
				<ul>
					<li>
						<img src="img/pic41.png" >
						<h4>微软人工智能-数据分析平台</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic41.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic41.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li>
						<img src="img/pic41.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
				<ul>
					<li class="fifth">
						<img src="img/pic41.png" >
						<h4>Android Hybrid APP开发实战 H5+原生!</h4>
						<h5>初级 <span>125人在学习</span></h5>
					</li>
				</ul>
			</div>
		</div>
		</div>
		<!-- 底部 --> 
		<div class="footer">
			<div class="banxin">
				<div class="copyright">
					<img src="img/header.png" >
					<h4>学成在线致力于普及中国最好的教育它与中国一流大学和机构合作提供在线课程。
© 2017XTCG Inc.保留所有权利。-ICP15025210</h4>
					<a href="#" class="app">下载APP</a>
				</div>
					<div class="links">
						<dl>
							<dt>关于学成网</dt>
							<dd><a href="#">关于管理团队</a></dd>
							<dd><a href="#">工作机会</a></dd>
							<dd><a href="#">客户服务</a></dd>
							<dd><a href="#">帮助</a></dd>
						</dl>
						<dl>
							<dt>新手指南</dt>
							<dd><a href="#">如何注册</a></dd>
							<dd><a href="#">如何选课</a></dd>
							<dd><a href="#">如何拿到毕业证</a></dd>
							<dd><a href="#">学分是什么</a></dd>
							<dd><a href="#">考试未通过怎么办</a></dd>
						</dl>
						<dl>
							<dt>合作伙伴</dt>
							<dd><a href="#">机构合作</a></dd>
							<dd><a href="#">合作导师</a></dd>
						</dl>
					</div>
				
				
			</div>
		</div>
	</body>
</html>

xuecheng.css的代码:
* {
	margin: 0;
	padding: 0;
}
body {
	background-color: #f3f5f7;
	/* height: 3000px; */
}
li {
	list-style: none;
}
a {
	text-decoration: none;
}
.banxin {
	width: 1200px;
	margin: auto;
	/* background-color: pink; */
}
/* 清除浮动 */
.clearfix:before,.clearfix:after {
content:"";
display:table;
}
.clearfix:after {
clear:both;
}
.clearfix {
*zoom:1;
}
.header {
	height: 42px;
	/* background-color: pink; */
	margin-top: 30px;
	margin-bottom: 30px;
}
.logo {
	width: 198px;
	margin-right: 70px;
	float: left;
}
.nav ul li{
	display: inline-block;
	float: left;
	margin: 0 15px;
}
.nav ul li a {
	line-height: 42px;
	text-decoration: none;
	height: 42px;
	padding: 0 10px;
	font-size: 18px;
	color: #050505;
	display: inline-block;
}
.nav ul li a:hover {
	border-bottom: 2px solid #00a4ff; 
	color: #00a4ff;
}
.search  {
	width: 412px;
	height: 42px;
	float: left;
	margin-left: 50px;
	/* background-color: #0000FF; */
}
#text {
	width: 345px;
	height: 40px;
	border: 1px solid #00a4ff;
	border-right: none;
	color: #dfe0e2;
	font-size: 14px;
	padding-left: 15px;
	float: left;
}
.search button {
	width: 50px;
	height: 42px;
	border: 0;
	float: left;
	background-color: #00a4ff;
}
.user {
	float: right;
	line-height: 42px;
	font-size: 14px;
	color: #666666;
	padding-right: 33px;
}
.banner {
	height: 420px;
	background-color: #1c036c;
}
.banner .banxin {
	height: 420px;
	background: url(../img/banner2.png) no-repeat top center;
}
.banner .banxin .subnav {
	float: left;
	width: 196px;
	height: 420px;
	background: rgb(0,0,0,.3);
}
.banner .banxin .subnav ul li {
	height: 46px;
	margin-left: 20px;
}
.banner .banxin .subnav ul li a {
	line-height: 46px;
	font-size: 14px;
	color: #ffffff;
	text-decoration: none;
}
.banner .banxin .subnav ul li a span {
	float: right;
	margin-right: 25px;
}
.banner .banxin .subnav ul li a:hover {
	color: #02a4ef;
}
.banner .banxin .course {
	float: right;
	margin-top: 50px;
	width: 230px;
	height: 300px;
	background-color: #ffffff;
}
.banner .banxin .course .coursetable {
	height: 50px;
	background-color: #9bceea;
	color: #ffffff;
	font-size: 18px;
	line-height: 50px;
	text-align: center;
}
.banner .banxin .course .first,
.banner .banxin .course .second,
.banner .banxin .course .third
{
	margin-top: 10px;
	margin-left: 20px;
	height: 50px;
	border-bottom: 1px solid #e7e7e7;
	font-size: 16px;
	color: #5a5a5a;
}
.banner .banxin .course .now {
	color: #bdbdbd;
}
.banner .banxin .course .all button {
	border: 1px solid #9cd2ff;
	width: 200px;
	height: 40px;
	background-color: #FFFFFF;
	margin-bottom: 13px;
	margin-left: 15px;
	margin-top: 10px;
}
.banner .banxin .course .all button a {
	color: #9cd2ff;
}
.recom {
	height: 60px;
	line-height: 60px;
	background-color: #ffffff;
	margin-top: 10px;
	box-shadow: 0 2px 3px 3px rgba(0,0,0,.1);
}
.recom ul {
	float: left;
}
.recom ul li {
	float: left;
}
.recom ul li a {
	padding: 0 30px;
	font-size: 16px;
	color: #050505;
	border-left: 1px solid #ccc;
}
.recom ul li a:hover {
	color: #00a4ff;
}
.recom span a {
	float: right;
	margin-right: 160px;
	font-size: 14px;
	color: #00a4ff;
}
.box {
	margin-top: 30px;
	height: 600px;
	background-color: #f3f5f7;
}
.box .box-hd h3 {
	float: left;
	font-weight: 400;
	height: 45px;
	font-size: 20px;
	color: #494949;
} 
.box .box-hd a {
	float: right;
	font-size: 12px;
	color: #a5a5a5;
	margin-right: 30px;
	margin-top: 10px;
}
.box .box-bd {
	float: left;
	height: 555px;
}
.box .box-bd ul {
	width: 1225px;
}
.box .box-bd ul li {
	float: left;
	width: 228px;
	height: 270px;
	background-color: #ffffff;
	margin-right: 15px;
	margin-bottom: 15px;
}
.box .box-bd ul li img {
	width: 100%;
}
.box .box-bd ul li h4 {
	font-weight: 400;
	height: 58px;
	font-size: 14px;
	color: #050505;
	padding-top: 15px;
	padding-left: 25px;
	padding-right: 20px;
}
.box .box-bd ul li h3 {
	font-weight: 400;
	padding-left: 25px;
	font-size: 12px;
	color: #ff7c2d;
}
.box .box-bd ul li h3 span {
	font-size: 12px;
	color: #999999;
}
.biancheng {
	margin-top: 37px;
	height: 472px;
	background-color: #f3f5f7;
}
/* .biancheng .banxin {
	width: 1350px;
} */
.biancheng .box-hd2 {
	height: 43px;
}
.biancheng .box-hd2 h3 {
	float: left;
	font-size: 20px;
	color: #494949;
	padding-right: 350px;
}
.biancheng .box-hd2 a {
	float: right;
	font-size: 12px;
	color: #a5a5a5;
	padding-right: 32px;
}
.biancheng .box-hd2 .center {
	float: left;
	/* font-size: 16px; */
	color: #868686;
}
.biancheng .box-hd2 .center span a {
	float: left;
	font-size: 16px;
	margin-right: 35px;
}
.biancheng .box-hd2 .center span a:hover {
	color: #afd7fa;
}
.biancheng .box-bd2 {
	height: 420px ;
	margin-top: 10px;
}
.biancheng .box-bd2 .left2 {
	float: left;
	width: 230px;	
	height: 420px;
	background-color: blue;
}
.biancheng .box-bd2 .right2 {
	float: right;
	width: 959px;
	height: 420px;
	background-color: #f3f5f7;
}
.biancheng .box-bd2 .right2 .righttop {
	height: 100px;
}
.biancheng .box-bd2 .right2 ul li {
	float: left;
	margin-top: 23px;
	width: 228px;
	height: 297px;
	background-color: #ffffff;
	margin-right: 15px;
}
.biancheng .box-bd2 .right2 ul .eight {
	margin-right: 0;
}
.biancheng .box-bd2 .left2 img {
	width: 100%;
	height: 100%;
}
.biancheng .box-bd2 .right2 .righttop img {
	width: 100%;
	height: 100%;
}
.biancheng .box-bd2 .right2 ul li img {
	width: 100%;
}
.biancheng .box-bd2 .right2 ul li h4 {
	font-weight: 400;
	font-size: 14px;
	color: #050505;
	padding-top: 25px;
	padding-left: 23px;
}
.biancheng .box-bd2 .right2 ul li h5 {
	padding-top: 20px;
	padding-left: 25px;
	font-size: 14px;
	color: #ff7c2d;
}
.biancheng .box-bd2 .right2 ul li h5 span {
	font-size: 14px;
	color: #999999;
}
.mestudy {
	padding-top: 37px;
	height: 351px;
	background-color: #f3f5f7;
}
.mestudy .box-hd3 {
	height: 81px;
}
.mestudy .box-hd3 h3 {
	float: left;
	font-size: 20px;
	color: #494949;
	padding-right: 350px;
	padding-bottom: 23px;
}
.mestudy .box-hd3 a {
	float: right;
	font-size: 12px;
	color: #a5a5a5;
	padding-right: 32px;
}
.mestudy .box-hd3 .center {
	float: left;
	/* font-size: 16px; */
	color: #868686;
}
.mestudy .box-hd3 .center span a {
	float: left;
	font-size: 16px;
	margin-right: 35px;
}
.mestudy .box-hd3 .center span a:hover {
	color: #afd7fa;
}
.mestudy .box-bd3 {
	float: left;
	height: 270px;
}
.mestudy .box-bd3 ul {
	width: 1225px;
}
.mestudy .box-bd3 ul li {
	float: left;
	width: 228px;
	height: 270px;
	background-color: #ffffff;
	margin-right: 15px;
	margin-bottom: 15px;
}
.mestudy .box-bd3 ul li img {
	width: 100%;
}
.mestudy .box-bd3 ul li h4 {
	font-weight: 400;
	font-size: 14px;
	color: #050505;
	padding-top: 15px;
	padding-left: 25px;
	padding-right: 20px;
}
.mestudy .box-bd3 ul li h5 {
	font-weight: 400;
	font-size: 12px;
	color: #ff7c2d;
	padding: 15px 20px 0 25px;
}
.mestudy .box-bd3 ul li h5 span {
	color: #999999;
}
.footer {
	height: 415px;
	margin-top: 40px;
	background-color: #ffffff;
}
.footer .banxin{
	padding-top: 35px;
}
.footer .banxin .copyright {
	float: left;
	width: 472px;
}
.footer .banxin .copyright h4 {
	margin-top: 25px;
	font-size: 12px;
	color: #666666;
}
.footer .banxin .copyright .app {
	display: block;
	width: 118px;
	height: 33px;
	border: 1px solid #00a4ff;
	text-align: center;
	line-height: 33px;
	font-size: 16px;
	color: #00a4ff;
	margin-top: 20px;
}
.footer .banxin .links {
	float: right;
}
.footer .banxin .links dl {
	float: left;
	margin-left: 100px;
}
.footer .banxin .links dl dt {
	font-size: 16px;
	color: #333333;
	margin-bottom: 5px;
}
.footer .banxin .links dl dd a {
	font-size: 12px;
	color: #333333;
} 

结果图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
06-03
这里是使用 Python 的 PyMySQL 完成上述任务的代码: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='test') # 创建游标 cur = conn.cursor() # 创建 users 表 cur.execute(''' CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(20), age INT ) ''') # 创建 orders 表 cur.execute(''' CREATE TABLE orders ( id INT PRIMARY KEY, order_date DATE, amount INT, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) ) ''') # 插入数据 users_data = [ (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28) ] orders_data = [ (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4) ] for user in users_data: cur.execute('INSERT INTO users VALUES (%s, %s, %s)', user) for order in orders_data: cur.execute('INSERT INTO orders VALUES (%s, %s, %s, %s)', order) # 提交事务 conn.commit() # 查询语句 # 1. 查询订单总金额 cur.execute('SELECT SUM(amount) FROM orders') print(cur.fetchone()[0]) # 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 cur.execute('SELECT ROUND(AVG(age), 2) FROM users') print(cur.fetchone()[0]) # 3. 查询订单总数最多的用户的姓名和订单总数。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_orders DESC LIMIT 1 ''') print(cur.fetchone()) # 4. 查询所有不重复的年龄。 cur.execute('SELECT DISTINCT age FROM users') print([row[0] for row in cur.fetchall()]) # 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 cur.execute('SELECT SUM(amount) FROM orders WHERE order_date BETWEEN "2021-09-01" AND "2021-09-04"') print(cur.fetchone()[0]) # 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id WHERE age <= 25 GROUP BY users.id ORDER BY total_orders DESC ''') print(cur.fetchall()) # 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 3 ''') print(cur.fetchall()) # 8. 查询订单总金额最大的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 1 ''') print(cur.fetchone()) # 9. 查询订单总金额最小的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount ASC LIMIT 1 ''') print(cur.fetchone()) # 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 cur.execute('SELECT * FROM users WHERE name LIKE "%李%" ORDER BY name ASC') print(cur.fetchall()) # 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 cur.execute('SELECT * FROM users WHERE age > 20 ORDER BY age DESC LIMIT 5') print(cur.fetchall()) # 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC ''') print(cur.fetchall()) # 关闭游标和连接 cur.close() conn.close() ``` 注意:在运行代码之前,需要先安装 PyMySQL 模块,可以使用以下命令进行安装: ``` pip install pymysql ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值