sqlserver数据库"子连接"count数量查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver数据库"子连接"count数量查询相关的知识,希望对你有一定的参考价值。

car_report 是 车牌主表, plate_number 字典是car_reportd 的主键
也是 exceed_report表 和 associat_report表的外键

下面两个表 是单独查询各自的每台车的数量
select plate_number,COUNT(*) as 超速 from exceed_report group by plate_number

insert into exceed_report(plate_number)
values('粤c10090')
insert into exceed_report(plate_number)
values('粤c10060')
insert into exceed_report(plate_number)
values('粤c11662')
insert into exceed_report(plate_number)
values('粤C12353')

exceed_report 是超速表

select plate_number,COUNT(*) as 超载 from associat_report group by plate_number
associat_report 是超载表

insert into associat_report(plate_number)
values('粤c10090')
insert into associat_report(plate_number)
values('粤c10060')

------------------------------------------------------------------------------------------------
请问哪位老师sql语句能达到这样的效果啊?学生我 sql语句一般 求教.....
车牌 超速 超载
1 粤c10060 1 1
2 粤c10090 1 1
3 粤c11662 1 0
4 粤C12353 1 0

如何能达到这样的效果呢? 求老师赐教 感谢

select a.plate_number 车牌,isnull(b.超速,0)超速,isnull(c.超载,0)超载 
from car_report a left join
(select plate_number,COUNT(*) as 超速 from exceed_report group by plate_number) b 
on a.plate_number=b.plate_number left join 
(select plate_number,COUNT(*) as 超载 from associat_report group by plate_number) c 
on a.plate_number=c.plate_number

参考技术A select a.*,b.超载 from

(select plate_number,COUNT(*) as 超速 from exceed_report group by plate_number) a left join
(select plate_number,isnull(COUNT(*),0) as 超载 from associat_report group by plate_number) b on a.plate_number=b.plate_number

关于jsp连接SQLserver 2000 的问题

我的代码~~~

<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"java.sql.*\"%>
<html>
<body>
<%
Class.forName(\"com.microsoft.jdbc.sqlserver.SQLServerDriver\").newInstance();
String url=\"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student\";
String user=\"stu\";
String password=\"stu\";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql=\"select * from T_User\";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) %>
<%=rs.getString(1)%>
<%%>
<%rs.close();
stmt.close();
conn.close();
%>

</body>
</html>

然后报错是下边的
我在我的lib下装了很多包~~我不知道是什么问题~~请好心的你帮帮我~~~

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /test.jsp(1,25) quote symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:204)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:154)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:166)
org.apache.jasper.compiler.ParserController.getPageEncodingForJspSyntax(ParserController.java:490)
org.apache.jasper.compiler.ParserController.determineSyntaxAndEncoding(ParserController.java:431)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:207)
org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:120)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:165)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.

--------------------------------------------------------------------------------

Apache Tomcat/6.0.20

我不知道怎么加200分 但是我会追加分数的

你把下面的代码的url和驱动改成sql2000的就好了,完整代码如下,记得jar驱动放好,如果输出的是内存地址的话就证明配置没问题,不然就是你的操作或者配置问题了:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;DATABASENAME=managerNews";
private static final String UNAME="sa";
private static final String UPASSWORD="sa";

/*
* 连接数据库
*/
public Connection getConnection()
Connection conn=null;
try
Class.forName(DRIVER_CLASS);
conn=DriverManager.getConnection(URL,UNAME,UPASSWORD);
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();


return conn;

/*
* 关闭conn,ps,rs,释放资源
*/
public void closeAll( Connection conn, PreparedStatement ps, ResultSet rs )
/* 如果rs不空,关闭rs */
if(rs != null)
try rs.close(); catch (SQLException e) e.printStackTrace();

/* 如果pstmt不空,关闭pstmt */
if(ps!= null)
try ps.close(); catch (SQLException e) e.printStackTrace();

/* 如果conn不空,关闭conn */
if(conn != null)
try conn.close(); catch (SQLException e) e.printStackTrace();



// public static void main (String[] args)
//
// BaseDao bo= new BaseDao();
// Connection conn=null;
// conn=bo.getConnection();
// System.out.print(conn);
//
//
参考技术A <%
try
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user="stu";
String password="stu";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from T_User";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
out.println(rs.getString(1)); //此处循环输出你想要的显示的

catch(Exception e)e.printStackTrace();
finally
rs.close();
stmt.close();
conn.close();

%>
参考技术B 把双引号前面的\去掉,全部的 参考技术C 将class那句改成这样: class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver");

以上是关于sqlserver数据库"子连接"count数量查询的主要内容,如果未能解决你的问题,请参考以下文章

SqlServer系列子查询

关于jsp连接SQLserver 2000 的问题

执行sql操作后如何获取其返回值

vs2014与sqlserver数据库连接出现错误,求大神解决,最好附有步骤截图

Servlet 连接SQLServer 问题

怎么用sqlserver连接远程数据库,详细步骤,谢谢各位