sql sum    空或0

sum   有时会返回空,有时返回0 

什么区别呢
sql <wbr>sum <wbr> <wbr> <wbr> <wbr>空或0
这样一个表. 
select sum(a) from TEST t where t.b <8     得到是0 
select sum(a) from TEST t where t.b <7     得到是空

通过where 找不到符合条件的记录. sum的结果就是空.
    where 中有符合的记录. 记录中该字段的和是0的时候结果是0 .

有时候我们希望找不到记录时得到 0 .
我觉得在sql上去处理不是个好的方式. 因为按逻辑确实没找到. 应该在业务层对这样的结果做处理. 毕竟在业务层里,找不到或找到0 这两种情况有的时候是有区别的. 

虽然不好但还是说下吧: 
oracle中找到空显示为0 是这样 select nvl   (sum(a),0)
mysql 中                     select isnull(sum(a),0)
create database mydatabase; use mydatabase; create table account(name varchar(20) primary key,password varchar(50) not null); insert into account(name,password) values('南京交通','njjt') ; select*from account; update account set password =md5(password); select*from account; package myPackage.examJavaBean; import java.sql.*; public class DatabaseConn { private static Connection conn =null; public static Connection getConnection(String url,String username,String password) { try { Class.forName("com.mysql.jdbc.Driver"); conn =DriverManager.getConnection(url,username,password); }catch(ClassNotFoundException e) { System.out.println(e); }catch(SQLException e){ System.out.println(e); } return conn; } public static void getClose(Connection conn,Statement ps,ResultSet rs){ try{ if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); }catch(SQLException e){ System.out.println(e); } } } <%@ page language="java" contentType="text/html; charset=GB18030"%> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <script src="/myPro/jQuery/jquery-3.6.4.min.js"></script> <script type="text/javascript"> if($("#user").focus()){ var veriUser=function(){ var user=$("#user").val(); if(user==null||user=="") $("#userMsg").html("输入用户名"); else $("#userMsg").html(""); }; } if($("#pwd").focus()){ var veriPwd=function(){ var pwd=$("#pwd").val(); if(pwd==null||pwd=="") $("#pwdMsg").html("输入登录密码"); else $("#pwdMsg").html(""); }; } $(function(){ $("#btn").bind("click",function(){ var user =$("#user").val(); var pwd=$("#pwd").val(); if(user==null||user==""||pwd==null||pwd=="") return false; }); }); </script> <body> <form id="form" name="form" method="post" action="/myPro/servlet/Servletexam"> <p>用户名:<input type="text" name="user" id="user" maxlength="12" onblur="veriUser()"/><em id="userMsg"></em></p> <p>密 &nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password"name="pwd"id="pwd"maxlength="12"onblur="veriPwd()"/><em id="pwdMsg"></em></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit"name="button"id="btn" value="提交"/>&nbsp;&nbsp; <input type="reset"name="button" value="取消"/></p></form> </body> </html> <%@ page language="java" contentType="text/html; charset=GB18030"%> <html> <head> <title>NewFile.jsp</title> </head> <style type="text/css"> h2{text-align:center} p{text-align:center;color:red} </style> <body> <h2>登录结果</h2> <p> 登录成功!</p> <p>欢迎你,<%=session.getAttribute("name")%></p> </body> </html> package myPackage.examServlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.codec.digest.DigestUtils; import myPackage.examJavaBean.DatabaseConn; @WebServlet("/servlet/Servletexam") public class Servletexam extends HttpServlet{ private static final long serialVersionUID=1L; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doPost(request,response); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ request.setCharacterEncoding("GB18030"); String name=request.getParameter("user"); String password=request.getParameter("pwd"); response.setContentType("text/html;charset=GB18030"); HttpSession session =request.getSession(); PrintWriter out =response.getWriter(); Connection conn =DatabaseConn.getConnection("jdbc:mysql://localhost:3306/myDatabase","root","mysql"); try{ PreparedStatement ps=conn.prepareStatement("select password from account where name =?"); ps.setString(1,name); ResultSet rs=ps.executeQuery(); if(rs. next()) { if(DigestUtils.md5Hex(password).equals(rs.getString(1))){ session.setAttribute("name", name); response.sendRedirect("/myPro/exam/success.jsp"); }else out. print("<script type=text/javascript>alert(' 密码错误!重新登录'); location='/myPro/exam/login.jsp'</script>"); }else out. print("<script type=text/javascript>alert(' 用户名错误!重新登录'); location='/myPro/exam/login.jsp'</script>"); DatabaseConn.getClose(conn,ps,rs); } catch(SQLException e){ System.out.println(e); } } } <%@ page language="java" contentType="text/html; charset=GB18030"%> <html> <body> <% out.print("这是一场考试"); %> </body> </html> <%@ page language="java" contentType="text/html; charset=GB18030"%> <html> <body> <% int sum=0; for( int i=1;i<=50;i++) sum +=i; %> <p>50以内正整数的和为<%=sum %></p> </body> </html>
最新发布
07-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值